python image library making dotty gifs

Steve Holden sholden at holdenweb.com
Fri Jun 25 08:32:27 EDT 2004


Alex Hunsley wrote:

> Alex Hunsley wrote:
> 
>> I'm using python image library 1.1.4 
>> (http://www.pythonware.com/products/pil/) to plot images.
>> However, when I save an image to a gif file, it comes out very 
>> dotty/ithered looking, even if it's a picture consisting only of one 
>> colour!
>>
>> Here's some example code that produces a dotty gif:
>>
>> #!/usr/bin/python
>>
>> # pymand joy!
>>
>> import Image
>> import ImageDraw
>>
>>
>> imageWidth=300
>> imageHeight=300
>>
>> im = Image.new("RGB", (imageWidth, imageHeight))
>>
>>
>> draw = ImageDraw.Draw(im)
>> for y in range (0,imageHeight):
>>     for x in range (0, imageWidth):
>>         draw.point((x, y), (128,128,128))
>>
>> # saving as a gif comes out dotty/dithered looking!
>> im.save("plotImage.gif", "GIF")
>>
>>
>> you can see the output gif here:
>>
>> http://ohmslaw.ogr.uk/plotImage.gif
> 
> 
> sorry, duff link, should be .org.uk:
> 
> http://ohmslaw.org.uk/plotImage.gif
> 
That's because PIL is storing the image as an indexed image, presumably 
using a palette that doesn't contain your exact color, and hence the 
software is dithering (that's a technical term, it doesn't mean the 
program can't make it's mind up ;-) to approximate the color you asked for.

Try using a color from the web-safe palette (google for it) or better 
still (even though the patents are now expired almost everywhere) use 
PNG instead.

regards
  Steve



More information about the Python-list mailing list