[IM7 bug?] `-background transparent -flatten` clears whole 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
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

[IM7 bug?] `-background transparent -flatten` clears whole image

Post by sas »

Here I crop an image beyond the original geometry, then flatten to preserve the new geometry created by the crop, and then add a border:

Code: Select all

magick rose: -crop 50x50+30+20! -flatten -bordercolor black -border 5x5 b.png
Image

So far so good.
But now I'd like to use Transparent instead of White as the background color for the extra area that was added by the crop. So I set the background option, but this causes the flatten operation to clear the entire image:

Code: Select all

magick rose: -crop 50x50+30+20! -background transparent -flatten -bordercolor black -border 5x5 b.png
Image

Is this a bug?
I'm relatively sure it didn't happen in IM6.

How else can I get it to do what I want? I'm starting to wonder if `-flatten` is the correct/best way to preserve the extended crop geometry.

--------------
Version: ImageMagick 7.0.3-0 Q32 x86_64 2016-09-07
Last edited by sas on 2016-09-10T00:07:35-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by snibgo »

With v6.9.5-3 and 7.0.2-5 and your second command ...

Code: Select all

%IMG7%magick rose: -crop 50x50+30+20! -background transparent -flatten -bordercolor black -border 5x5 b7.png
... I get what seems the correct result, with transparency over the white are in your first example.

I don't know why you have "!" in "-crop", or what that might do. You might try inserting "+repage" after the crop. Does that help?

It looks like a bug. Or, umm, undocumented new feature.
snibgo's IM pages: im.snibgo.com
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by sas »

snibgo wrote:I don't know why you have "!" in "-crop", or what that might do.
It's what allows me to crop beyond the original image geometry and keep the extra areas.

From the docs: "By adding a exclamation character flag to the geometry argument, the cropped images virtual canvas page size and offset is set as if the geometry argument was a viewport or window. This means the canvas page size is set to exactly the same size you specified, the image offset set relative top left corner of the region cropped."
snibgo wrote:You might try inserting "+repage" after the crop. Does that help?
On the contrary, `+repage` seems to remove the viewport information entirely rather than making it "stick".
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by sas »

Turns out I can make it work in IM7 by explicitly setting -alpha on before the -flatten:

Code: Select all

magick rose: -crop 50x50+30+20! -alpha on -background transparent -flatten -bordercolor black -border 5x5 b.png
Image

Is this intended behavior, then?
Or should it be reported as a bug?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by snibgo »

Ah, well done, you've figured it out. I had forgotten that in v7, an image doesn't automatically have alpha. This is a documented feature.

"-alpha set" is preferred to "-alpha on". See documentation http://www.imagemagick.org/script/comma ... .php#alpha , which no longer even lists "-alpha on".
snibgo's IM pages: im.snibgo.com
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by sas »

I see.

Still seems weird that this would cause "-background transparent -flatten" to clear the whole image, doesn't it?

It's as if it treats the image without alpha channel as all-transparent, when it would make more sense to treat it as all-opaque, or alternatively throw an error message about the lack of alpha channel.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by snibgo »

sas wrote:... when it would make more sense to treat it as all-opaque ...
I agree. I made a similar comment on another thread. I don't understand why IM v7 sometimes treats an image with no alpha as if it was entirely transparent.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: [IM7 bug?] `-background transparent -flatten` clears whole image

Post by GeeMack »

sas wrote:Still seems weird that this would cause "-background transparent -flatten" to clear the whole image, doesn't it?.
This command (IM 7.0.3 on Windows 10 64) outputs an example of the fully transparent image...

Code: Select all

magick -background none -bordercolor green ^
   -size 240x240 ( gradient:white-blue -page +20+20 ) ^
   -size 120x120 ( gradient:red-yellow -page +70+70 ) ^
   -flatten -border 2x2 sample1.png
Image

The suggested solution is to add "-alpha on" immediately before the "-flatten" operation like this...

Code: Select all

magick -background none -bordercolor green ^
   -size 240x240 ( gradient:white-blue -page +20+20 ) ^
   -size 120x120 ( gradient:red-yellow -page +70+70 ) ^
   -alpha on -flatten -border 2x2 sample2.png
Image

That provides the result I might have expected from the first command example above. The two gradient images are composited to their "-page" positions with a 20 pixel transparent background showing on the left and top. Here's where it gets confusing...

Adding the "-edge 4" operation after "-flatten", the entire image goes transparent again.

Code: Select all

magick -background none -bordercolor green ^
   -size 240x240 ( gradient:white-blue -page +20+20 ) ^
   -size 120x120 ( gradient:red-yellow -page +70+70 ) ^
   -alpha on -flatten -edge 4 -border 2x2 sample3.png
Image

Oddly enough, if I remove the "-border" operation, there's a 4 pixel wide strip around the upper left of the white-blue gradient area. It's a gradient, not the sort of line I normally expect from the "-edge" operator, and it leaves everything else in the image fully transparent again.

Code: Select all

magick -background none -bordercolor green ^
   -size 240x240 ( gradient:white-blue -page +20+20 ) ^
   -size 120x120 ( gradient:red-yellow -page +70+70 ) ^
   -alpha on -flatten -edge 4 sample4.png
Image

If I do "-alpha off" again before the "-edge" operation like this...

Code: Select all

magick -background none -bordercolor green ^
   -size 240x240 ( gradient:white-blue -page +20+20 ) ^
   -size 120x120 ( gradient:red-yellow -page +70+70 ) ^
   -alpha on -flatten -alpha off -edge 4 -border 2x2 sample5.png
Image

... the result is more like I'd expect. Somehow it doesn't seem like it should be necessary to turn "-alpha on" or "off" before and after each operation that affects the alpha channel.

As always, maybe I'm misunderstanding or overlooking something.
Post Reply