Resizing Tif's to smaller gifs adds pixels

Scott David Daniels Scott.Daniels at Acm.Org
Tue Oct 21 16:46:31 EDT 2008


smullen.uclick at gmail.com wrote:
> ...I need to scale a TIFF image from 1925x588 px to a GIF of 600xnnn px.
> I've tried the following code, but it leads to a lot of extra odd
> colored pixels being inserted into the resulting image.

> img = "tmp/tmsho20080901.tif"
> im = Image.open("tmp/tmsho20080901.tif")
> w, h = im.size
> im.thumbnail((600, h * 600 / w), Image.ANTIALIAS)
This line is rather silly, making a result that you drop on the floor.
> newimg = im.resize((600, int(h * (600.0 / w))), Image.ANTIALIAS)
> newimg.save("tmsho20080901.gif")
...

> Using ImageMagick's convert I would do this...
> 
> convert -colors 256 -resize 600 -colorspace RGB -black-threshold 100 -
> contrast -intent Perceptual tmp/tmsho20080901.tif tmsho20080901.gif
> 
> I think it may have something to do with the palette or the number of
> colors alotted for the resulting image, but I'm really not a graphics
> guy.
Yep, you are right.  The issue is trying to reduce to a 256-color 
pallette.  Try using '.png' format (portable network graphics), that
allows a full palette for exact pixel images.

Try something like this:
     im = Image.open("tmp/tmsho20080901.tif")
     w, h = im.size
     im.thumbnail((600, h * 600 // w),
                  Image.ANTIALIAS).save("tmp/sho20080901.png")

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list