By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

image resize question

P: n/a
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
Oct 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Oct 18, 11:56 am, "Tim Arnold" <tim.arn...@sas.comwrote:
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

Oct 18 '07 #2

P: n/a
"Matimus" <mc******@gmail.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
On Oct 18, 11:56 am, "Tim Arnold" <tim.arn...@sas.comwrote:
>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
Thank you! The ANTIALIAS filter works great. With any of the others, I still
lost my reference lines.
--Tim
Oct 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.