Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python's PIL, how do I change the quality of an image? [closed]

I want to degrade the quality of the image to a few kilobytes. What's the best way to do this?

Thanks!

like image 458
TIMEX Avatar asked Dec 04 '10 10:12

TIMEX


People also ask

How do I reduce image quality in Python?

getsize(image_name) # print the size before compression/resizing print("[*] Size before compression:", get_size_format(image_size)) if new_size_ratio < 1.0: # if resizing ratio is below 1.0, then multiply width & height with this ratio to reduce image size img = img. resize((int(img. size[0] * new_size_ratio), int(img.


2 Answers

If the picture format is JPEG, here's an example:

from PIL import Image
im = Image.open("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg")
im.save("C:\Users\Public\Pictures\Sample Pictures\Jellyfish_compressed.jpg", quality=10)

The references you need to be reading are:

  • [The Image module][1], particularly the "save" function, which allows you to pass in options relevant for each image format.
    • Each image format's options are in a different page, you can find it in the docs.
like image 179
Asim Ihsan Avatar answered Oct 05 '22 09:10

Asim Ihsan


Solved.

I did....

im.save( blah, quality=5)
like image 44
TIMEX Avatar answered Oct 05 '22 08:10

TIMEX