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.
Answer from Ryan G on Stack Overflowpython - How to reduce the image file size using PIL - Stack Overflow
Trying to compress images with PIL but the file size isn't changing.
Question: Why does it take so long for the Image Save node to save huge PNG files? Is this unique to ComfyUI, this node, or PNGs in general?
Need to resize multiple images to a specific size in KB. How do I achieve this?
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.
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?