Convert png to png8 and detect background color

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
imagemcr
Posts: 3
Joined: 2016-07-03T04:10:02-07:00
Authentication code: 1151

Convert png to png8 and detect background color

Post 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.
Last edited by imagemcr on 2016-07-04T00:17:13-07:00, edited 2 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert png to png8 and detect background color

Post 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.
snibgo's IM pages: im.snibgo.com
imagemcr
Posts: 3
Joined: 2016-07-03T04:10:02-07:00
Authentication code: 1151

Re: Convert png to png8 and detect background color

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert png to png8 and detect background color

Post 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
imagemcr
Posts: 3
Joined: 2016-07-03T04:10:02-07:00
Authentication code: 1151

Re: Convert png to png8 and detect background color

Post by imagemcr »

Thanks fmw42, I will see which of these i can use until they fix other programs to do this.
Post Reply