python image library making dotty gifs

A

Alex Hunsley

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

what causes the dottyness, anyone know?

alex
 
A

Alex Hunsley

Alex said:
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
 
O

Oliver Fromme

Alex Hunsley said:
> 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!
> [...]
> im = Image.new("RGB", (imageWidth, imageHeight))
> [...]
> im.save("plotImage.gif", "GIF")

You're creating an RGB image (i.e. 24 bits per pixel), and
then you're saving it to a GIF file which can handle only
8 bits per pixel. The GIF file format uses a so-called
palette to map 24bit RGB colors to 8 bits. You haven't
defined any palette, so I guess it's using a default one,
to which it has to convert your image, obviously involving
dithering.

I'm not a PIL user, so I can't tell you how to define your
own optimized palette with it.

Another option would be to save the image in PNG format,
which supports 24bits natively.

Best regards
Oliver
 
S

Steve Holden

Alex said:
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
 
A

Alex Hunsley

Oliver said:
Alex Hunsley said:
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!
[...]
im = Image.new("RGB", (imageWidth, imageHeight))
[...]
im.save("plotImage.gif", "GIF")

You're creating an RGB image (i.e. 24 bits per pixel), and
then you're saving it to a GIF file which can handle only
8 bits per pixel. The GIF file format uses a so-called
palette to map 24bit RGB colors to 8 bits. You haven't
defined any palette, so I guess it's using a default one,
to which it has to convert your image, obviously involving
dithering.

I'm not a PIL user, so I can't tell you how to define your
own optimized palette with it.

Another option would be to save the image in PNG format,
which supports 24bits natively.

Best regards
Oliver
aha! ok. thanks to you and steve for your help! :)
alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top