Making corners of an image transparent

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mndrue

Making corners of an image transparent

Post by mndrue »

I have a bunch of jpg's that I would like to convert to png's and also make their corners transparent. That is, I want the images to look like someone cut off the corners.

So, I though perhaps I could use an overlay. However, I assume that if I set say a white image with the transparent corners as the background and overlay the new image, it won't work. And obviously I can't use the original as the background and then overlay transparentness, or can i?

Other suggestions?

TIA,
drue
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Making corners of an image transparent

Post by Bonzo »

Is this the sort of effect you want - this code creates a rounded rectangle and uses that to cut the corners. You can use other shapes for the mask.
Image

Code: Select all

convert -size 637x140 xc:none -fill white-draw "roundRectangle 0,0 637,140 15,15" albir.jpg -compose SrcIn -composite rounded_corners.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making corners of an image transparent

Post by fmw42 »

Not sure what you really want. But you can mask the corners to transparent or draw transparent boxes at the corners.

Make a white image and overlay small black images in its corner where you want it to be transparent. Then use that as an alpha channel for your image.

convert -size 128x128 xc:white white.png
convert -size 16x16 xc:black black.png
convert white.png \
black.png -gravity northwest -compose over -composite \
black.png -gravity northeast -compose over -composite \
black.png -gravity southeast -compose over -composite \
black.png -gravity southwest -compose over -composite \
mask.png

Image

convert zelda3.png mask.png -compose copy_opacity -composite zelda3_corners.png

Image
mndrue

Re: Making corners of an image transparent

Post by mndrue »

*blush*, ok, this is pretty geeky, but here's what I'm aiming for:

Image

becomes something like this, except those corners are of course transparent, not just white:

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making corners of an image transparent

Post by fmw42 »

You can draw one big polygon filled white with black or transparent on the outside like Bonzo did with his rounded corner or you can make one small square image like I did, but make diagonally half black and half white (you will have to use -draw to make the triangular sections) and then composite as I did in the corners on a white image to make the alpha channel.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Making corners of an image transparent

Post by anthony »

Make sure you enable the alpha channel if using Alpha composition.

However if using CopyOpacity make sure to turn it off (or it copies a opaque alpha rather than the grayscale mask).

A similar method is detailed in IM Examples. Thumbnails, Rounded Corners, the only difference between that and what you want is the shape of the drawn mask being used.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making corners of an image transparent

Post by fmw42 »

Here is my triangle drawing on a square for the corner method:

convert -respect-parenthesis \( -size 128x128 xc:white \) \
\( -size 16x16 xc:black -fill white -draw "polygon 0,0 0,15 15,15" -write mpr:triangle +delete \) \
mpr:triangle -gravity northeast -compose over -composite \
\( mpr:triangle -rotate 90 \) -gravity southeast -compose over -composite \
\( mpr:triangle -rotate 180 \) -gravity southwest -compose over -composite \
\( mpr:triangle -rotate -90 \) -gravity northwest -compose over -composite \
zelda3.jpg +swap -compose copy_opacity -composite \
zelda3_corners2.png

Image


However, it may be easier/quicker to just find the polygon you want and draw one mask.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Making corners of an image transparent

Post by anthony »

No need to create a large mask, if you use a transparent background you can alpha compose
the corners directly on the main image. this way you don't even need to know the images size!

Code: Select all

convert thumbnail.gif -alpha set -compose DstOut \
        \( -size 20x10 xc:none -draw "polygon 0,0 0,9 19,0" \
           -write mpr:triangle  +delete \) \
        \( mpr:triangle             \) -gravity northwest -composite \
        \( mpr:triangle -flip       \) -gravity southwest -composite \
        \( mpr:triangle -flop       \) -gravity northeast -composite \
        \( mpr:triangle -rotate 180 \) -gravity southeast -composite \
        corner_cutoff.png

I have added it to the rounded corner thumbnail examples
http://www.imagemagick.org/Usage/thumbnails/#rounded
It will appear in an hour or so.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mndrue

Re: Making corners of an image transparent

Post by mndrue »

Wow. I am just floored by everyone's level of support and helpfulness here. Thank you so much, it's perfect. I had to update imagemagick to pick up -alpha, but it wasn't too bad.

drue
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Making corners of an image transparent

Post by anthony »

-matte will do the same job as -alpha set and has been around as long as I can remember.

The -alpha method is the newer one that expands the role of the older option with lots more options
see http://www.imagemagick.org/Usage/basics/#alpha
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply