5

I would like to quantize a 24bit image to 16bit color depth using Python Imaging.

PIL used to provide a method im.quantize(colors, **options) however this has been deprecated for out = im.convert("P", palette=Image.ADAPTIVE, colors=256)

Unfortunately 256 is the MAXIMUM number of colors that im.convert() will quantize to (8 bit only).

How can I quantize a 24bit image down to 16bit using PIL (or similar)?

thanks

Rich
  • 7,146
  • 1
  • 23
  • 25
  • 3
    I don't think PIL supports 16-bit graphics... http://www.pythonware.com/library/pil/handbook/concepts.htm#mode – Ignacio Vazquez-Abrams Dec 03 '10 at 12:20
  • 2
    24 bit in terms of colour normally means 8 bit per colour - i.e. 8 bits red, 8 bits green and 8 bits blue. 16 bit colour depth will normally mean 16 bits per colour - i.e. you are increasing not decreasing the colour depth. I don't know if PIL handles this or not. – neil Dec 03 '10 at 14:59
  • 16-bit images are not supported by PIL, regardless of whether you mean 16 bits total (usually 3 bits each RGB, sometimes 1-bit alpha, sometimes 4-bit G) or 16 bits each RGB. The available modes are listed here: http://www.pythonware.com/library/pil/handbook/concepts.htm – kindall Dec 03 '10 at 23:17
  • by 24bit I mean true color or 16777216 colors (25,256,256). By 16 bit I mean high color or 65536 colors or (32,32,32) or (32,64,32) - doesn't matter to me. – Rich Dec 04 '10 at 01:28
  • PIL has experimental support for 16-bit pixels. Search the source for "I;16" mode. I've been using it for a while. – David Poole Jun 20 '11 at 15:57

1 Answers1

3

You might want to look into converting your image to a numpy array, performing your quantisation, then converting back to PIL.

There are modules in numpy to convert to/from PIL images.

pisswillis
  • 1,569
  • 2
  • 14
  • 19
  • This is looking like a good solution. I also notice that OpenCV might be more useful than PIL for image processing here. – Rich Dec 04 '10 at 01:29