Page 1 of 1

Making corners of an image transparent

Posted: 2009-07-01T10:38:01-07:00
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

Re: Making corners of an image transparent

Posted: 2009-07-01T11:04:54-07:00
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

Re: Making corners of an image transparent

Posted: 2009-07-01T11:07:23-07:00
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

Re: Making corners of an image transparent

Posted: 2009-07-01T14:05:15-07:00
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

Re: Making corners of an image transparent

Posted: 2009-07-01T15:37:13-07:00
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.

Re: Making corners of an image transparent

Posted: 2009-07-01T18:33:02-07:00
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.

Re: Making corners of an image transparent

Posted: 2009-07-01T19:51:00-07:00
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.

Re: Making corners of an image transparent

Posted: 2009-07-01T20:25:40-07:00
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.

Re: Making corners of an image transparent

Posted: 2009-07-01T21:50:05-07:00
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

Re: Making corners of an image transparent

Posted: 2009-07-01T23:37:56-07:00
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