Page 1 of 1

Convert png to png8 and detect background color

Posted: 2016-07-03T04:16:57-07:00
by imagemcr
I want to convert a 24bit images png. bmp etc to 8bit png and always detect the background color and set it as color #1 of the palette in a color map, so that it automatically become sthe binary transparency of an image. In the past irfanview did that but now it always gives me a wrong color:

is there a command that can preserve to good quality a Png, and always detect the background color, if not detect the pixel at the upper left corner, i can always set it to the color i want. Command line is fine, gui is even better. I decided to try this when irfanview doesn't do it right. For the png quality some programs like gimp do not make that good quality png, I like irfanview's quality when you decrease color depth and i wonder of this imagemagick can do the same quality and palette as i like.

Re: Convert png to png8 and detect background color

Posted: 2016-07-03T04:51:32-07:00
by snibgo
... and set it as color #1 of the palette in a color map, ...
As far as I know, there is no way to tell IM what colours to place in particular slots in the palette map.

Re: Convert png to png8 and detect background color

Posted: 2016-07-03T05:23:26-07:00
by imagemcr
Programs like iview and others when you save a PNG or GIF it detects the background color and sets it as the color to use for transparency, usually puts it as first color. I thought transparent-color can do something but if it can't then fine, i will look for another program.

Re: Convert png to png8 and detect background color

Posted: 2016-07-03T10:59:03-07:00
by fmw42
I believe you can set the PNG8 transparency by using -transparent yourbackgroundcolor. But I am not an expert on PNG. The PNG developer will be able to recommend the proper method.

I do not know how to detect the background color of an image automatically (other than getting the histogram and finding the most frequent color, see http://www.imagemagick.org/Usage/files/#histogram. You can have IM choose any pixel you want or just specify it yourself.

Try

Code: Select all

convert yourimage.png +dither -colors 256 -transparent somecolor PNG8:result.png
That works using the IM internal image logo:

Code: Select all

convert logo: +dither -colors 256 -transparent white PNG8:logo8.png
To get the color in the top left corner (unix syntax for IM 6)

Code: Select all

color=`convert logo: +dither -colors 256 -format "%[pixel:u.p{0,0}]" info:`
convert logo: +dither -colors 256 -transparent "$color" PNG8:logo8.png

Re: Convert png to png8 and detect background color

Posted: 2016-07-03T23:49:23-07:00
by imagemcr
Thanks fmw42, I will see which of these i can use until they fix other programs to do this.