[Image-SIG] 3-bit color palette

inhahe inhahe at gmail.com
Sun Mar 30 03:03:18 CEST 2008


i don't know if this is what you wanted, but i wrote something to do it..
---
import Image

def to8bit(im):
  im = im.convert('RGB')
  data = im.tostring()
  s = ''.join(('\x00\xFF'[x>='\x80'] for x in data))
  return Image.fromstring('RGB',im.size, s).convert('P')

to8bit(Image.open('RGB_24bits_palette_color_test_chart.png')).save('
colortest.png')
to8bit(Image.open('RGB_24bits_palette_sample_image.jpg')).save('parrot.png')
---
reproduces the results on wikipedia exactly, as far as i can tell


On Sat, Mar 29, 2008 at 8:28 PM, Gokce Taskan <gokcetaskan at gmail.com> wrote:

> I've been searching all over the internet for a method to convert an
> image to a 8 color (3-bit) image. I've used these:
>
> Based on brightness but not good results:
> palette = []
> for i in range(32):
>    palette.extend([0, 0, 0])
> for i in range(32):
>    palette.extend([0, 0, 255])
> for i in range(32):
>    palette.extend([0, 255, 0])
> for i in range(32):
>    palette.extend([0, 255, 255])
> for i in range(32):
>    palette.extend([255, 0, 0])
> for i in range(32):
>    palette.extend([255, 0, 255])
> for i in range(32):
>    palette.extend([255, 255, 0])
> for i in range(32):
>    palette.extend([255, 255, 255])
>
> Outputs a grayscale image because it fills the rest of the palette with
> black, grays and whites:
> palette = [(0,0,0)] * 8
> palette[1] = (0, 0, 255)
> palette[2] = (0, 255, 0)
> palette[3] = (0, 255, 255)
> palette[4] = (255, 0, 0)
> palette[5] = (255, 0, 255)
> palette[6] = (255, 255, 0)
> palette[7] = (255, 255, 255)
>
> I am trying find a method to output a resonable image which would look
> like the one in
> http://en.wikipedia.org/wiki/List_of_monochrome_and_RGB_palettes#3-bit_RGB
>
> Do you have any advices?
>
> Thank you,
>
> Gokce
>
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/image-sig/attachments/20080329/5f46eb48/attachment.htm 


More information about the Image-SIG mailing list