Page 1 of 1

Transparent geometric shapes

Posted: 2015-06-26T06:51:06-07:00
by snibgo
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.

Re: Transparent geometric shapes

Posted: 2015-06-26T08:58:39-07:00
by fmw42
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)

Re: Transparent geometric shapes

Posted: 2015-06-26T10:09:44-07:00
by snibgo
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.

Re: Transparent geometric shapes

Posted: 2015-06-26T10:45:29-07:00
by fmw42
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.

Re: Transparent geometric shapes

Posted: 2015-06-26T11:05:38-07:00
by snibgo
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?

Re: Transparent geometric shapes

Posted: 2015-06-26T11:07:12-07:00
by fmw42
snibgo wrote:Any thoughts on whether the OP shows a bug, or a feature?
I think the IM developers will need to answer that.