开发者_如何转开发
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this questionI 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:
- [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.
Solved.
I did....
im.save( blah, quality=5)
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
精彩评论