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

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Kazuhiko
Posts: 2
Joined: 2011-10-20T06:19:56-07:00
Authentication code: 8675308

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

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

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Kazuhiko
Posts: 2
Joined: 2011-10-20T06:19:56-07:00
Authentication code: 8675308

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

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

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

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