Page 1 of 1
Adding a transparent circle to an image?
Posted: 2009-10-27T13:23:30-07:00
by pwnedd
Hi all,
I was wondering if anyone has any suggestions on the fastest way to extract a circle of a given radius and offset from an existing image. One way to do this would be to first create a separate binary image with the same dimensions, draw a circle,
and then using something like "-compose copy_opacity -composite" to specify the transparent region. IM is very feature-rich, however, so I am wondering if there are other more efficient ways (e.g. drawing a transparent circle on the image original image?), that would be more efficient.
Any suggestions? Any help would be greatly appreciated.
Thanks!
Re: Adding a transparent circle to an image?
Posted: 2009-10-27T13:33:17-07:00
by Bonzo
I think you need to create a mask. This code will do what you want but you do not say how you are intending on running it e.g. comandline, php etc. but you should be able to work something out from it. You can probably combine it all into one line.
Code: Select all
<?php
// Create the round mask
exec("convert -size 266x200 xc:none -fill black -draw \"circle 140,100 140,30\" mask.png");
// cut the mask shape out of the image
$cmd = "-compose dst-out mask.png $input -matte ";
exec("composite $cmd compose_dst_out.png");
// Delete the mask
unlink("mask.png");
?>
Re: Adding a transparent circle to an image?
Posted: 2009-10-27T13:51:15-07:00
by pwnedd
Hi Bonzo,
Thanks for the quick response! I'll actually being using IM command-line through PHP, so your example is perfect.
I appreciate the help
Take care!
Re: Adding a transparent circle to an image?
Posted: 2009-10-27T14:00:23-07:00
by fmw42
I may be wrong, but I don't think IM allows you to draw a transparent area on an image. You can use matte fill in draw to fill some color with transparency, but then you have to draw some none unique color and then fill it. So I think the drawing of a black area on a white image and overlaying it as the transparency channel is the best way to go.
Re: Adding a transparent circle to an image?
Posted: 2009-10-27T17:47:08-07:00
by anthony
If the image already contains transparency, then the 'DstOut' technique is the only way to do it.
Basically you have the 'painters' problem. You can paint a window, but once painted you can not 'paint transparency'. The DstOut composition is sort of like creating a shape and using that to cut through the previously applied paint.
Their is no current 'erase' brush or anything like that in the current Draw operator. Then again the same is true in the Postscript, and SVG drawing methods.
Of course other techniques are also usefull for this. specifically cloning and making a transparent canvas the same size as the original image to 'draw' the 'erase' mask. For example, here I draw a 'moon crescent' on an image, which is best generated on a separate image, where I can 'cut out a piece of it).
Code: Select all
convert logo: -alpha on \
\( +clone -alpha Transparent -fill skyblue -draw 'circle 35,35 35,5' \
\( +clone -alpha Transparent -fill black -draw 'circle 28,30 35,5' \) \
-compose Dst_Out -composite \) \
-compose Over -composite show:
This shows clearly the full scope of the 'layered image' technique.
You can even take this further by coloring the overlay with some random pattern using other composition techniques.
NOTE replace the logo: with whatever you like, and show: with whatever file name you like.
Re: Adding a transparent circle to an image?
Posted: 2009-10-28T12:56:05-07:00
by pwnedd
Thanks for the example Anthony. I've never used to clone parameter before, but it looks like it could be very useful.
Basically you have the 'painters' problem. You can paint a window, but once painted you can not 'paint transparency'. The DstOut composition is sort of like creating a shape and using that to cut through the previously applied paint.
That is exactly my case. Basically I am starting with an 8-bit grayscale image with no transparent components. I want to then cut out a circle from somewhere in the image (apply some other operations), and then save the image as a transparent PNG.
Re: Adding a transparent circle to an image?
Posted: 2009-10-28T15:08:14-07:00
by fmw42
try this. it creates a grayscale gradient, then creates a white mask of the size of the gradient (via -threshold) with a black circle in the middle, then overlays the mask as the alpha channel to the gradient.
You want to process the original image in any way you want, then overlay the transparent hole from the mask, not the the other way around.
convert \( -size 100x100 gradient: \) \
\( +clone -threshold -1 -fill black -draw "circle 50,50 75,50" \) \
-alpha off -compose copy_opacity -composite grad_hole.png
Re: Adding a transparent circle to an image?
Posted: 2009-12-02T12:28:43-07:00
by pwnedd
If the image already contains transparency, then the 'DstOut' technique is the only way to do it.
Sorry to bring this one back up after so long. I just wanted to be sure: is there a better way to do this if the image does *not* have any transparency prior to be acted on?
Thanks!
Re: Adding a transparent circle to an image?
Posted: 2009-12-02T12:38:32-07:00
by fmw42
I don't know any way other than one of the composite methods discussed before. You cannot simply draw transparency on an image. At least not currently as far as I know.
Re: Adding a transparent circle to an image?
Posted: 2009-12-02T12:41:12-07:00
by pwnedd
Okay thanks. Just wanted to check. Thanks as always for the reply
Re: Adding a transparent circle to an image?
Posted: 2009-12-02T17:49:36-07:00
by anthony
pwnedd wrote:If the image already contains transparency, then the 'DstOut' technique is the only way to do it.
Sorry to bring this one back up after so long. I just wanted to be sure: is there a better way to do this if the image does *not* have any transparency prior to be acted on?
Thanks!
If the image does NOT have transparent then use a grayscale mask and CopyOpacity. This image composition method is really only useful in that situation as it completely replaces the alpha (opacity) channel of the image with the grayscale mask given. For this to work ALL input images should have alpha turned off, which is the only recommended usage of CopyOpacity
If alpha is NOT turned off it copies the alpha channel of the mask to the alpha channel of the image which for an input image conatining no transparency is equivalent to a DstIn, which is not what people are generally wanting.