Page 1 of 1

convert transp. img to jpg with '-geometry' results black bg

Posted: 2011-10-20T06:28:51-07:00
by Kazuhiko
Hello.

When we convert a transparent image to jpeg, its background color differs depending on the existence of '-geometry' (or -resize) option.

Reproducible: Always

Steps to Reproduce:
1. convert transparent.png out.jpg => white background
2. convert -geometry 320x transparent.png out.jpg => black background

Expected Results:
Same background color regardless the existence of '-geometry' (or -resize) option.

I confirmed this issue with ImageMagick 6.7.3-1, but it does not happens in ubuntu 11.10's ImageMagick 6.6.0-4.

Regards,
Kazuhiko

Re: convert transp. img to jpg with '-geometry' results blac

Posted: 2011-10-20T20:07:56-07:00
by anthony
JPEG does not save transparency, so what you see is the color of transparent pixels.! Yes all pixel have color, even 'transparent' ones.. and many image formats save that hidden information!

Some operations replace that transparent color with some other color, typically black. Resize is an example of this, as you pointed out.

The best idea is to remove the transparency from the image before saving to JPEG.

See IM examples, Masking and Background Removal, Removing Transparency
http://www.imagemagick.org/Usage/masking/#remove

For PNG images setting all fully-transparent pixels to a specific color (any color) will typically aid in the images compression, and final disk size. See section immediatally above the last one..
IM Examples, Masking and Background Removal, Alpha Background Color
http://www.imagemagick.org/Usage/maskin ... background

Re: convert transp. img to jpg with '-geometry' results blac

Posted: 2011-10-21T00:45:59-07:00
by Kazuhiko
Hi Anthony,

Thanks for your detailed reply.
Indeed, adding '-background white -flatten' makes white-background jpeg regardless the existence of '-resize' option. (Well, even only '-flatten' makes white-background jpeg).

BTW, I found this when I tried to convert a PDF that was generated by LibreOffice. But I wonder if treating PDF as a 'transparent' format is a good idea or not. Do you think that we can automatically 'flatten' PDF input ?

Kazuhiko

Re: convert transp. img to jpg with '-geometry' results blac

Posted: 2011-10-21T04:54:49-07:00
by anthony
Just be careful. -flatten will merge multiple images into one image.
-border 0 however will remove transparency from a whole list of images without merging.

BOTH however are effected by other options such as compose, or virtual canvas.


FUTURE...

I am thinking that we really should have a -alpha remove option that will do a faster 'underlay' of the background color over all images. A bit like -border 0 does but without the -compose influence (always merge colors using 'over' blending), or the creation of a second canvas image. IT could also turn off alpha (though not remove or modify it) when finished!

In otherwords a dedicated transparency removal with special alpha preserving features, if such is needed.