4

I've been trying to save 8-bit PNGs (PNG8) using RMagick (http://rmagick.rubyforge.org/) and ChunkyPNG (https://github.com/wvanbergen/chunky_png) but have been unable to do so.

However the only time I can get it to work on RMagick is if the ImageMagick installation is based on the QuantumDepth of 8 rather than the usual 16. It is not possible to change this setting on the fly - the installation of ImageMagick has to be compiled with this setting. Also setting the depth to 8 when creating the image or prefixing a format type when saving have no effect.

ie. img.write('PNG8:image.png')

Anyway I've had a look at ChunkyPNG and I really prefer to use this over RMagick - simply because it is pure Ruby and doesnt depend on ImageMagick. I can't save a PNG8 using that too.

I have to convert the PNG to 8bit afterwards using a graphics program -

My questions:

  1. Is there a way of saving 8bit PNGs properly like it does on ImageMagick Q8 on a machine with ImageMagick Q16 installed?

  2. Can anyone provide pointers as to do my own 4-bit encoder in ChunkyPNG or know of a way to save PNG8 with it?

Thanks in advance..

anami
  • 152
  • 1
  • 7

1 Answers1

5

What exactly do you mean by PNG8? 8-bit grayscale, 8-bit indexed color, 3x8 bit RGB or 4x8 bit RGBA? All of these color modes are supported by ChunkyPNG.

By default, ChunkyPNG tries to determine the best color mode to save your image. You can overwrite it by providing an options hash to the save method:

image.save('filename.png', color_mode: ChunkyPNG::COLOR_TRUECOLOR)
# Or: ChunkyPNG::COLOR_TRUECOLOR_ALPHA    

image.to_blob(color_mode: ChunkyPNG::COLOR_INDEXED, bit_depth: 8)

More info: https://github.com/wvanbergen/chunky_png/wiki

wvanbergen
  • 2,294
  • 15
  • 15
  • Forgive me for not being clear. I think from your description it would be 8-bit indexed colour. I'm creating a new image in code and saving that in the smallest PNG format possible.. – anami Jun 28 '11 at 11:45
  • 'img.save("public/image.png", :color_mode => ChunkyPNG::COLOR_INDEXED, :bit_depth => 8)' is giving me this exception: 'ChunkyPNG::ExpectationFailed at / This palette has too many colors! * file: png_encoding.rb * location: encode_png_pixelstream * line: 157 ' Converting the image into indexed PNG via GIMP is possible and works for my purpose. – anami Jun 28 '11 at 12:06
  • 8-bit indexed color mode only supports up to 256 colors (2^8). You will need to reduce the number of colors in your image to make PNG8 work. ChunkyPNG will automatically pick indexed color mode if you're using 256 colors or less. – wvanbergen Jun 28 '11 at 13:19
  • Before your reply - I realised what I was doing and it's wrong. You are correct - the palette is too big. Quite a wayward way to find out. Thank you for your help and patience. It looks like ChunkyPNG has won over RMagick! – anami Jun 28 '11 at 17:08