Converting a 32-bit PNG to an 8- or 4-bit PNG, command line

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
HaLo2FrEeEk

Converting a 32-bit PNG to an 8- or 4-bit PNG, command line

Post by HaLo2FrEeEk »

I'm using the Commandline version of ImageMagick on my server running GNU/Linux and Apache. According to ImageMagick I'm using version "ImageMagick 6.3.7 04/15/09 Q16."

I'd like to convert a supplied 32-bit PNG (meaning 8-bits per channel over 4 channels: red, green, blue, and alpha. 4 * 8 = 32) to an 8-bit (meaning 2 bits per channel) and 4 bit (1-bit per channel) PNG. There's no way this is impossible, but I'm finding LOADS of trouble accomplishing it on the internet, and having a fair amount of it myself as well.

Here is what looked most promising:

http://www.imagemagick.org/Usage/formats/#png_write

See where it says "-define", well there's a lot more information there than on the main ImageMagick -define description. I thought I'd found exactly what I needed when I saw the table for "-define png:color-type". I thought I'd simply set my color type to 3 for indexed, which allows a bit depth of 1, 2, 4, or 8, then set my bit-depth to 2. Well, no go, since define will only work if you're not performing a lossy conversion, since 32-bit to 8-bit is lossy, it refuses to do anything at all. I also tried "-colors 256" because 8-bit images have 256 colors. Well, it reduced the number of colors to 256....but made it show up as a 64-bit image (16 bits per channel)!!! That's not right.

What the heck am I doing wrong? By all logic, there should be a simple function to reduce the bit-depth of an image, but it seems impossible. Someone, please help me!

EDIT: Here are some example images...

I applied this command to both of these examples:

convert {input image} -colors 256 {output image}

This is my main image, the one with the alpha, this is my main goal, to get this image to 8-bit (total) depth, here is the original:

http://infectionist.com/images/misc/ext ... on_256.png

And here is after applying the command:

http://infectionist.com/images/misc/ext ... _256_8.png

Here is my another test I did, using a screenshot from a game cutscene, original:

http://infectionist.com/images/misc/Cinematic1_0818.png

And after the command:

http://infectionist.com/images/misc/Cin ... 0818_8.png

As you can see, the colors ARE reduced, but the originals are showing up as 32 and 24-bit respectively (because the second image has no alpha, it's only 8-bit-per-channel * 3 channels), and the converted images are showing up as 64- and 48-bit respectively, obviously something is wrong here.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting a 32-bit PNG to an 8- or 4-bit PNG, command line

Post by fmw42 »

Don't know if IM can help for all your request. But use -depth 8 to force to 8-bits per channel on Q16 otherwise, you get 16-bits per channel. Also see -type palette or palettematte to set to use a color palette for 8 bits total (with alpha for matte)

Also see http://www.imagemagick.org/Usage/formats/#png_formats for info about PNG output formats supported by IM.

Also your 6.3.7 is very old (over 200 versions old). you may want to consider an upgrade.
HaLo2FrEeEk

Re: Converting a 32-bit PNG to an 8- or 4-bit PNG, command line

Post by HaLo2FrEeEk »

Shared hosting, upgrade isn't in my control. I'll send an email to my host though.
Post Reply