image resize question

Matimus mccredie at gmail.com
Thu Oct 18 17:04:37 EDT 2007


On Oct 18, 11:56 am, "Tim Arnold" <tim.arn... at sas.com> wrote:
> Hi, I'm using the Image module to resize PNG images from 300 to 100dpi for
> use in HTML pages, but I'm losing some vertical and horizontal lines in the
> images (usually images of x-y plots).
>
> Here's what I do:
> import Image
> def imgResize(self,filename):
>         img = Image.open(filename)
>         dpi = img.info.get('dpi')
>         if dpi and 295 < int(dpi[0]) < 305:
>             wd = img.size[0]/3.0 #convert from 300dpi to 100dpi
>             ht = img.size[1]/3.0
>             newimg= img.resize((int(wd),int(ht)))
>             newimg.save(filename)
>
> imgResize('myimage.png')
>
> Can someone point me to a better way so I don't lose the reference lines in
> the images?
> thanks,
> --Tim Arnold

Resize accepts a second parameter that is used to determine what kind
of downsampling filter to use (http://www.pythonware.com/library/pil/
handbook/image.htm). The default is Image.NEAREST, which just samples
the nearest pixel and results in the type of data loss you are seeing.
If you want something better try one of the following and see which
works best for you: Image.BILINEAR, Image.BICUBIC or Image.ANTIALIAS.

example:
...
    newimg = img.resize((int(wd),int(ht)),Image.ANTIALIAS)
...

Matt




More information about the Python-list mailing list