There are two parts to JPEG quality. The first is the quality setting which you have already set to the highest possible value.
JPEG also uses chroma subsampling, assuming that color hue changes are less important than lightness changes and some information can be safely thrown away. Unfortunately in demanding applications this isn't always true, and you can most easily notice this on red edges. PIL didn't originally expose a documented setting to control this aspect.
Pascal Beyeler discovered the option which disables chroma subsampling. You can set subsampling=0 when saving an image and the image looks way sharper!
im.save('/path/to/cover-2.jpg', format='JPEG', subsampling=0, quality=100)
The Pillow project took over where PIL left off and made many improvements, including documenting the formerly undocumented subsampling option. It's been enhanced to take either an integer or string argument, but I still recommend 0 as shown above.
https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg
Note also that the documentation claims quality=95 is the best quality setting and that anything over 95 should be avoided. This may be a change from earlier versions of PIL.
Quality degradation due to PIL.Image.save
How to adjust the quality of a resized image in Python Imaging Library? - Stack Overflow
Trying to compress images with PIL but the file size isn't changing.
Python PIL, preserve quality when resizing and saving - Stack Overflow
Use PIL's resize method manually:
image = image.resize((x, y), Image.ANTIALIAS) # LANCZOS as of Pillow 2.7
Followed by the save method
quality_val = 90
image.save(filename, 'JPEG', quality=quality_val)
Take a look at the source for models.py from Photologue to see how they do it.
ANTIALIAS is in no way comparable to the "85" quality level. The ANTIALIAS parameter tells the thumbnail method what algorithm to use for resampling pixels from one size to another. For example, if I have a 3x3 image that looks like this:
2 2 2
2 0 2
2 2 2
and I resize it to 2x2, one algorithm might give me:
2 2
2 2
because most of the pixels nearby are 2s, while another might give me:
1 1
1 1
in order to take into account the 0 in the middle. But you still haven't begun to deal with compression, and won't until you save the image. Which is to say that in thumbnailing, you aren't dealing with gradations of quality, but with discrete algorithms for resampling. So no, you can't get finer control here.
If you save to a format with lossy compression, that's the place to specify levels of quality.
new_size = (image_width_int,image_height_int)for individual_image in image_files:im = Image.open(r"resize_images/" + individual_image)resized_image = im.resize(new_size)resized_image.save('resized_images/' + individual_image, quality=85, optimize=True)
Even if I change the quality to 10, the files still come out the same size as if I didn't set the quality or optimize at all. Any ideas?
A built-in parameter for saving JPEGs and PNGs is optimize.
from PIL import Image
foo = Image.open('path/to/image.jpg') # My image is a 200x374 jpeg that is 102kb large
foo.size # (200, 374)
# downsize the image with an ANTIALIAS filter (gives the highest quality)
foo = foo.resize((160,300),Image.ANTIALIAS)
foo.save('path/to/save/image_scaled.jpg', quality=95) # The saved downsized image size is 24.8kb
foo.save('path/to/save/image_scaled_opt.jpg', optimize=True, quality=95) # The saved downsized image size is 22.9kb
The optimize flag will do an extra pass on the image to find a way to reduce its size as much as possible. 1.9kb might not seem like much, but over hundreds/thousands of pictures, it can add up.
Now to try and get it down to 5kb to 10 kb, you can change the quality value in the save options. Using a quality of 85 instead of 95 in this case would yield: Unoptimized: 15.1kb Optimized : 14.3kb Using a quality of 75 (default if argument is left out) would yield: Unoptimized: 11.8kb Optimized : 11.2kb
I prefer quality 85 with optimize because the quality isn't affected much, and the file size is much smaller.
lets say you have a model called Book and on it a field called 'cover_pic', in that case, you can do the following to compress the image:
from PIL import Image
b = Book.objects.get(title='Into the wild')
image = Image.open(b.cover_pic.path)
image.save(b.image.path,quality=20,optimize=True)
hope it helps to anyone stumbling upon it.
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)