Page 1 of 1

PNG to JPEG: how is background color determined?

Posted: 2018-08-16T13:57:19-07:00
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!

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

Posted: 2018-08-16T15:14:46-07:00
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?

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

Posted: 2018-08-16T15:27:26-07:00
by donahchoo
see below

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

Posted: 2018-08-16T15:42:10-07:00
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

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

Posted: 2018-08-16T15:48:04-07:00
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.

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

Posted: 2018-08-16T15:56:56-07:00
by donahchoo
Unfortunately, I can't post the source images. I think this is enough information to figure out what's happening. thanks.

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

Posted: 2018-08-16T16:04:27-07:00
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.

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

Posted: 2018-08-16T17:20:11-07:00
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.)