Adding a transparent circle to an image?

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
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Adding a transparent circle to an image?

Post 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!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Adding a transparent circle to an image?

Post 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"); 
?> 
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Adding a transparent circle to an image?

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a transparent circle to an image?

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding a transparent circle to an image?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Adding a transparent circle to an image?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a transparent circle to an image?

Post 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
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Adding a transparent circle to an image?

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a transparent circle to an image?

Post 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.
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Adding a transparent circle to an image?

Post by pwnedd »

Okay thanks. Just wanted to check. Thanks as always for the reply :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Adding a transparent circle to an image?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply