470,648 Members | 1,511 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

python image library making dotty gifs

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

Jul 18 '05 #1
4 2238
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

Jul 18 '05 #2
Alex Hunsley <la**@tardis.ed.ac.molar.uk> 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!
[...]
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

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)
Jul 18 '05 #3
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
Jul 18 '05 #4
Oliver Fromme wrote:
Alex Hunsley <la**@tardis.ed.ac.molar.uk> 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!
> [...]
> 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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.