[Image-SIG] Poor Image Quality When Resizing a GIF

Andy McCurdy sedrik at gmail.com
Wed Jan 9 00:42:54 CET 2008

Hi all,

Just started using PIL.  I'm attempting to resize a .gif image to a
smaller size, keeping the height/width proportional.  The resize
works, but the resulting image is very grainy.  Resizing the same
image with ImageMagick's convert utility produced a far better quality
image.  I'd rather use PIL if possible since it has a much better API.
 The below code works fine with JPG files.  Am I missing something?

    from PIL import Image

    # original_content is a string of bytes representing the image.
    # the image is not stored locally on disk, so it's fetched from
the storage service as a string
    # and loaded up using a StringIO buffer
    # Have also tried loading the same .gif from a local disk directly
with Image.open('filename.gif'), but image after resize looked grainy
as well
    io = StringIO(original_content)
    pil_image = Image.open (io)

    # docs suggest that the default mode, 'P', won't use the ANTIALIAS
filter, so switch to RGB
    pil_image = pil_image.convert('RGB')

    # ... stuff happens here to figure out what the new width and
height should be

    # tuple representing the new size
    new_size = (new_width, new_height)

    # the actual resize...
    pil_image = pil_image.resize(new_size, Image.ANTIALIAS)

    # save the new image back out to a StringIO buffer -- has to be
sent back to the storage service
    io = StringIO()
    format = 'GIF'   # hardcoded in this example
    pil_image.save(io, format)

Thanks for any help possible.

More information about the Image-SIG mailing list