Controlling background color of transparent PNG?

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
mattias

Controlling background color of transparent PNG?

Post by mattias »

Is there any way to change the background color of a transparent png?

The thing I'm after is a way to make my generated transparent png "work" in IE 6.

If I generate transparent png with IM they show with a black background in IE6 - if I do it from Photoshop the png have the background color #E7DFE7.

Is there anyway to make the background white in IE6?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Controlling background color of transparent PNG?

Post by fmw42 »

if your background is actually transparent, then try

convert image.png -fill white -opaque none output.png

But then all transparent areas will become white
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Controlling background color of transparent PNG?

Post by anthony »

PNG will save the color of the transparent pixels.

For example see the last example in
Solid Color Canvases, Transparent Canvas
http://imagemagick.org/Usage/canvas/#transparent
which turns of the transparency in the saved PNG making the transparent color visible again.

the problem is that just about every operator in IM will make any fully-transparent color fully-transparent black (or 'none'), as this is the way operation mathematics works, AND a fully-transparent color is by definition undefined!!!

As such if you really really really need to set the transparent color of a image you will need to do the following....
1/ Replace any instance of 'none' with the desired color (eg: "-fill '#CCCCCC00' -opaque none" ). Due to the way IM compares colors replacing 'none' will naturally match ALL fully-transparent colors, and NOT just fully-transparent black.

2/ save the image NOW, without doing any other image operation!

For example, using the 'test.png' image from IM examples
See http://imagemagick.org/Usage/#PNG
I can do the following...

Code: Select all

convert test.png -fill '#CCCCCC00' -opaque none image.png
Now I can check that image using

Code: Select all

convert image.png +matte miff:- | display
which verifies that fully transparent was set to a light grey color rather than black.

I am adding this to IM examples at
http://imagemagick.org/Usage/formats/#png_www
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mattias

Re: Controlling background color of transparent PNG?

Post by mattias »

Sounds great. I just can't seem to make it work. I use RMagick so I tried

img = img.opaque('none', "#CCCCCC00")

Which gives me the same result - black bg. I can make it work with your example but not with RMagick.

Any ideas?

Cheers
Mattias
mattias

Re: Controlling background color of transparent PNG?

Post by mattias »

If I try one of my generated png files the convert way also fails. This is an example of how my script looks like:

Code: Select all

images = Magick::Image.read("caption:Some text goes here") {
  self.size = "200x"
  self.pointsize = 18
  self.background_color = 'none'
  self.density = '144x144'    		  	
  self.fill = 'red'
}
    
img = images.first  
img = img.resample(72.0)
img.trim!(true)
   
img.write('test.png')  
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Controlling background color of transparent PNG?

Post by anthony »

I have no idea about RMagick.
Could your argument order be wrong?

Note you could use (assuming your call is correct)

Code: Select all

img = img.opaque('#CCCCCC00', "#CCCCCC00")
and still have it come out correctly. Remember ANY fully transparency color will always match ALL fully transparent colors.

However that C++ code will fail because
1/ You don't set the background color, as I showed you
2/ you do other operations that would cause it to be reset to black.

Remember to IM a fully-transparent pixel is invisible and as such mathematically its color does not matter at all. The color is just a 'unknown' color of no consequence, and due to the way things work defaults back to 'nothing' which means all zero values or black.

Only gray-scale channel operators (negate, histogram adjustment, etc) and direct pixel replacement type operators would not reset colors of such pixels to zero.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Controlling background color of transparent PNG?

Post by anthony »

Problem resolved. For Opaque to replace transparent pixels, you must ensure you also set -channel RGBA or the equivalent in your API.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mattias

Re: Controlling background color of transparent PNG?

Post by mattias »

After a lot of testing back and forth I finally got it working. The trick with RMagick was to set the background to transparent white to start with.

Code: Select all

images = Magick::Image.read("caption:Some text goes here") {
  self.size = "200x"
  self.pointsize = 18
  self.background_color = '#FFFFFF00'
  self.density = '144x144'               
  self.fill = 'red'
}
    
img = images.first  
img = img.resample(72.0)
img.trim!(true)
   
img.write('test.png')  
Thanks for all the help.
Mattias
pepe.cortisona

Re: Controlling background color of transparent PNG?

Post by pepe.cortisona »

The solution to this problem is much simpler than stated before!

The only thing you have to do, is to set the default background color like this:

> convert source -background "#cccccc" target

This leaves the transparency untouched and has the effect you were looking for. If the application which displays the image supports PNG transparency, it will use it (the background color will not be displayed). If not, the background color will be displayed (IE6 will use this color for the background).
Post Reply