[Image-SIG] How to generate optimal palettes ?

Chris Cogdon chris at cogdon.org
Tue Aug 12 23:42:22 EDT 2003


Hi folks! Hoping someone can help me with this problem. I did search the 
archives and it doesn't seem to have been covered in the past year.

I'm using PIL to generate thumbnails for my (rather large) artwork site. Using 
the 'thumbnail' method seems to work fine except for reduced-palette GIF 
images, where the resultant thumbnail looks all 'blocky'.

I determined that PIL is using the same low-colour palette as the input GIF, 
whereas regenerating the palette to use more colours would return a 
much-nicer-looking thumbnail. (Currently using ImageMagick, but I want to 
switch).

I figure that I'll specialcase paletted files and write code such as the 
following:

im1 = Image.load ( image_path )
im2 = im1.convert ( 'RGB' )
im2.thumbnail ( (128,128), Image.ANTIALIAS )
im3 = im2.convert ( 'P' )
im3.save ( thumb_path )

However, in this case, a really ugly palette is chosen and the image is 
visibly dithered. What I need is something that examines the image and 
chooses an optimal palette. (Lots of graphics applications do this already)
Does anyone have any suitable code? Does it exist in PIL and I've just missed 
it? How does one apply a palette when converting to 'P' mode ? Is it possible 
to opt to NOT dither during the conversion ?

Thanks kindly for considering my problem.

--------------------
Background:

I run a very heavily utilised community artwork site, with around 250,000 
images, 3,500 artists, 4 million hits per day over two separated servers. 
(Lots of test-fodder there, if people are interested :)

I was looking at PIL about 2.5 years ago, but found that 1.1.2 didn't support 
anti-aliased resizing, which is criticial to me. Instead, I used PIL for 
image mode detection, but ImageMagick to generate the thumbnails. This is 
both slow (calling an external program) and error prone (every upgrade to 
ImageMagick has broken something or another, and calling external scripts can 
play havoc with special characters in filenames). I'd LOVE to come back to 
PIL, but I have a few niggly problems to sort out, such as above, before I 
make the switch.

I'm very happy to run test code over the image collection, if someone wants to 
feed me beta code :)

PS: A hello to those also on db-sig :)

-- 
   ("`-/")_.-'"``-._        Chris Cogdon <chris at cogdon.org>
    . . `; -._    )-;-,_`)
   (v_,)'  _  )`-.\  ``-'
  _.- _..-_/ / ((.'
((,.-'   ((,/   fL



More information about the Image-SIG mailing list