PNG to JPEG: how is background color determined?

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
donahchoo
Posts: 8
Joined: 2018-07-20T13:38:38-07:00
Authentication code: 1152

PNG to JPEG: how is background color determined?

Post by donahchoo »

Hi,

I'm converting some PNGs to JPEG using "-alpha background". With some images the background color is white and with others it is black. When I use identify -verbose I get "Background color: white" for both sets of images.

Code: Select all

ImageMagick-6.9.3-1/bin/convert -resize 60x60> -alpha Background \
-sampling-factor 4:4:4 -colorspace sRGB -quality 80 -strip -write  60x60.jpeg 
How does image magick determine the background color?

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

Re: PNG to JPEG: how is background color determined?

Post by fmw42 »

You must specify -background somecolor before using -alpha background. But those commands simply change the color of the background in the base image for which the pixels are transparent. The default for -background is white, as I recall. Is this what you are trying to do?
donahchoo
Posts: 8
Joined: 2018-07-20T13:38:38-07:00
Authentication code: 1152

Re: PNG to JPEG: how is background color determined?

Post by donahchoo »

see below
Last edited by donahchoo on 2018-08-16T15:42:25-07:00, edited 1 time in total.
donahchoo
Posts: 8
Joined: 2018-07-20T13:38:38-07:00
Authentication code: 1152

Re: PNG to JPEG: how is background color determined?

Post by donahchoo »

fmw42 wrote: 2018-08-16T15:14:46-07:00 You must specify -background somecolor before using -alpha background. But those commands simply change the color of the background in the base image for which the pixels are transparent. The default for -background is white, as I recall. Is this what you are trying to do?
I'm trying to reimplement so old tools. At this point I need to understand why convert is using a black background for some images and white for others when writing JPEGs, so that my do this same way in the new tools...

I actually find this post where you said the background color is "usually" black... viewtopic.php?t=24048#p102562
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNG to JPEG: how is background color determined?

Post by fmw42 »

Your link to the post is not accessible. So I cannot look at it. It may be that typically the background color under transparent regions is black and when you save to JPG it will show black. But if you use -background white -alpha background and save to JPG, that should create a white background. The -color none or transparent always is equivalent the color "rgba(0,0,0,0)". So here the underlying color is black. But if you use "rgba(255,255,255,0)" you still have a transparent region/pixel, but when the alpha is turned off, it shows white "rgb(255,255,255)".

If you have an example input and output that you question, then please post the to some free hosting service such as dropbox.com that will not change the format and then put the URLs here so we can examine your images and test them. This forum does not allow direct uploads of images.
donahchoo
Posts: 8
Joined: 2018-07-20T13:38:38-07:00
Authentication code: 1152

Re: PNG to JPEG: how is background color determined?

Post by donahchoo »

Unfortunately, I can't post the source images. I think this is enough information to figure out what's happening. thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNG to JPEG: how is background color determined?

Post by fmw42 »

P.S. the background color in identify -verbose may be totally unrelated to what I have described above. I am not sure what the identify -verbose background color actually means. One of the developers or other users would have to comment with more definitive information.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG to JPEG: how is background color determined?

Post by snibgo »

I think the background color given by "identify -verbose" is the pseudo-color in the GIF palette that is used for transparent pixels. In any case, it isn't necessarily the color that "-alpha background" will use. If you care about that, always set "-background" first.

(Remember that "-alpha background" changes only fully-transparent pixels, not partly-transparent pixels. If you are saving to JPEG, "-layers flatten" may be more useful.)
snibgo's IM pages: im.snibgo.com
Post Reply