Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Image Library produces a crappy quality jpeg when I resize a picture

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
like image 242
TIMEX Avatar asked Dec 08 '22 04:12

TIMEX


1 Answers

Documentation sayyyyys:

im.save(thumbnail_file, 'JPEG', quality=90)
like image 196
Jonathan Feinberg Avatar answered May 18 '23 12:05

Jonathan Feinberg