PIL thumbnails unreasonably large

Larry Bates larry.bates at websafe.com
Wed May 10 11:22:55 EDT 2006


Almad wrote:
> Hello,
> 
> I wonder how do I create reasonable thumbnails from JPEG with PIL.
> My code:
> 
>             logging.debug('Downloading image %s' % id)
>             uri = ''.join([config['photo']['masterpath'], '?p=',
> str(id)])
>             uf = urlopen(uri).read()
>             f = tmpfile()
>             f.write(uf)
>             f.seek(0)
>             logging.debug('Resizing image %s' % id)
>             img = Image.open(f)
>             prev = img.copy()
>             img.thumbnail((180,180))
>             if prev.size[0] > 640 or prev.size[1] > 640:
>                 prev.thumbnail((640,640))
>             # save resized to temporary files
>             f.seek(0)
>             img.save(f, "JPEG", quality=50)
>             fp = tmpfile()
>             prev.save(fp, "JPEG", quality=200)
> 
> Well, works fine, but img size is about 0,5 MB (!), but strangely, prev
> one is around 200 kb.
> 
> How do I change this? I tried to play with various quality settings,
> but with no effect. img thumbnail is supposed to be < 10 kb...
> 
> Thank You, 
> 
> Almad
> 
JPEG quality 200 is overkill, btw -- it completely disables JPEG's
quantization stage, and "mainly of interest for experimental pur-
poses", according to the JPEG library documentation, which
continues:

"Quality values above about 95 are NOT recommended for
normal use; the compressed file size goes up dramatically
for hardly any gain in output image quality."

-Larry Bates



More information about the Python-list mailing list