4

I use the Python Image Library (PIL) to resize an image and create a thumbnail. Why is it that my code produces an image that is so crappy and low-quality? Can someone tell me how to modify the code so that it's the highest quality JPEG?

def create_thumbnail(buffer, width=100, height=100):
    im = Image.open(StringIO(buffer))
    if im.mode not in ('L', 'RGB', 'RGBA'):
        im = im.convert('RGB')
    im.thumbnail((width, height), Image.ANTIALIAS)
    thumbnail_file = StringIO()
    im.save(thumbnail_file, 'JPEG')
    thumbnail_file.seek(0)
    return thumbnail_file
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

13

Documentation sayyyyys:

im.save(thumbnail_file, 'JPEG', quality=90)
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
2

Hope this might help someone:

from PIL import Image
image = Image.open("2.jpg")
image.thumbnail((256, 256), Image.ANTIALIAS)
image.save("11.jpg", quality=100)
Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
Dinu Duke
  • 185
  • 13
  • Generally speaking, saving a jpg with "100% quality" doesn't noticeably increase the quality over, say, 90%, but just increases the filesize. – Xiong Chiamiov Dec 10 '16 at 07:53