I want to degrade the quality of the image to a few kilobytes. What's the best way to do this?
Thanks!
I want to degrade the quality of the image to a few kilobytes. What's the best way to do this?
Thanks!
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:
a) change the size: Image.resize(size, filter)
b) explicitly convert it to JPEG (if it is not) and set the desired quality. c) use a combination of a) and b)
Whatever you do, there is a trade-off between size and quality.
This worked for me to use a For Loop to resize images using PIL. The variable PRODUCTS is a list that had all product names in it, but you can also use readlines() for each line in a file to do so:
def resize_images(self):
products = PRODUCTS
for name in products:
try:
fp = open(filename + name + ".jpg", "rb")
img = Image.open(fp)
img.load()
img.save(filename + name + "_2" + ".jpg", quality=23)
fp.close()
except:
print name