Transparent geometric shapes

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
snibgo
Posts: 10360
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Transparent geometric shapes

Post by snibgo » 2015-06-26T06:51:06-07:00

Until v6.9.0-0, we could make a geometric area transparent by negating alpha, drawing the shape, and inverting alpha again. For example, to make a transparent circle, Windows BAT syntax:

Code: Select all

%IMG690%convert ^
  -size 200x200 xc:Blue ^
  -alpha set ^
  -fill Blue ^
  -channel A -negate ^
  -draw "circle 100,100 100,149" ^
  -negate +channel ^
  trans_c1.png
Image

From v6.9.1-0 (and possibly earlier), to the current v6.9.1-6, this no longer works. The same command:

Code: Select all

%IMG6916%convert ^
  -size 200x200 xc:Blue ^
  -alpha set ^
  -fill Blue ^
  -channel A -negate ^
  -draw "circle 100,100 100,149" ^
  -negate +channel ^
  trans_c2.png
Image
Drawing the circle has changed values of pixels that are outside the circle. Pixels that were transparent blue are changed to transparent black.

This looks like a bug to me, but perhaps there is a good reason.

So my questions are:

1. Is this a bug or a feature?

2. What is a simple but reliable method of making an arbitrary geometric shape transparent? I'm sure I'm missing something obvious.
snibgo's IM pages: im.snibgo.com

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

Re: Transparent geometric shapes

Post by fmw42 » 2015-06-26T08:58:39-07:00

You are making it too hard. Try these:

Code: Select all

convert -size 200x200 xc:blue ^
-fill white -draw "circle 100,100 100,149" ^
-fuzz 10% -transparent white ^
result.png

Code: Select all

convert -size 200x200 xc:blue ^
-fill white -fuzz 10% -draw "circle 100,100 100,149" ^
-fill none -draw "matte 100,100 floodfill" ^
result.png
adjust the fuzz factor to avoid aliasing (where there is mix of blue and white)

snibgo
Posts: 10360
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Transparent geometric shapes

Post by snibgo » 2015-06-26T10:09:44-07:00

That's a good solution if the base image doesn't have any white pixels, and if I am happy with a binary transparency.

In the general case, the base image might have any colours, and I want anti-aliased alpha.
snibgo's IM pages: im.snibgo.com

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

Re: Transparent geometric shapes

Post by fmw42 » 2015-06-26T10:45:29-07:00

snibgo wrote:That's a good solution if the base image doesn't have any white pixels, and if I am happy with a binary transparency.

In the general case, the base image might have any colours, and I want anti-aliased alpha.
Fair enough! Thanks for pointing that out.

So here is a better solution that handles with your objections. I should have posted that earlier.

Code: Select all

convert -size 200x200 xc:blue ^
( +clone -fill white -colorize 100% ^
-fill black -draw "circle 100,100 100,149" ) ^
-alpha off -compose copy_opacity -composite result.png
The -size 200x200 xc:blue can be replaced with any image.

snibgo
Posts: 10360
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Transparent geometric shapes

Post by snibgo » 2015-06-26T11:05:38-07:00

Thanks. And when the input may contain transparency, we can extract it, draw on the extraction, and CopyOpacity it back:

Code: Select all

convert ^
  -size 200x200 xc:blue ^
  ( +clone -alpha Extract ^
    -fill black -draw "circle 100,100 100,149" ) ^
  -alpha off -compose copy_opacity -composite ^
  result.png
When the geometric shape is a rectangle, a simpler way is to use region and set alpha to zero:

Code: Select all

convert ^
  -size 200x200 xc:Blue ^
  -alpha set ^
  -region 100x100+50+50 ^
  -channel A -evaluate set 0 +channel ^
  +region ^
  x1.png
These seems to be the easiest ways.

Any thoughts on whether the OP shows a bug, or a feature?
snibgo's IM pages: im.snibgo.com

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

Re: Transparent geometric shapes

Post by fmw42 » 2015-06-26T11:07:12-07:00

snibgo wrote:Any thoughts on whether the OP shows a bug, or a feature?
I think the IM developers will need to answer that.

Post Reply