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 Overflow
๐ŸŒ
DEV Community
dev.to โ€บ khatrivinay โ€บ compress-images-size-with-python-4ipj
Compress Images size with Python - DEV Community
August 27, 2022 - To use this library first we need to install for our Python environment using the pip install command. ... After installing this library we can start compressing the image we want. import PIL from PIL import Image #open the image with ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-pillow-compress-image
Python Pillow - Compress Image
To compress an image using Pillow, call Image.save() method on the Image object, and specify True for optimize parameter, and an integer value for quality parameter. In this tutorial, we shall go through examples on how to compress an image, and calculate compression rate for the images.
Discussions

python - How to reduce the image file size using PIL - Stack Overflow
I am using PIL to resize the images there by converting larger images to smaller ones. Are there any standard ways to reduce the file size of the image without losing the quality too much? Let's sa... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Trying to compress images with PIL but the file size isn't changing.
The images are probably already optimized and compressed. Why wouldn't they be? You may get a few percent better if you use a program specifically designed to optimize compression, which PIL is not. More on reddit.com
๐ŸŒ r/learnpython
8
6
December 15, 2022
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?
It uses Python pillow to save the PNG: https://github.com/python-pillow/Pillow/issues/1211 https://github.com/python-pillow/Pillow/issues/5986 : Saving PNG images with PIL is 4 times slower than saving them with OpenCV For whatever reason, PIL implements its own PNG filtering (as opposed to use something like libpng). The code lacks any SIMD, and looks relatively unoptimised (e.g. multiple passes over the data without any cache blocking), so even if you set zlib compression to 0, it's still awfully slow, because most of the CPU time is spent on filtering. PNG also use Deflate compression which is not the best. You could try to use this: https://github.com/qrmt/fpng-python in server.py (replacing original_pil.save(filepath, compress_level=4, pnginfo=metadata)) with this library https://github.com/veluca93/fpnge on your system (Luca Versari is a Google guy who worked on JPEG XL). Or you know you could save your picture in another format like JPEG XL or AVIF. I remember doing this locally for my Stable Diffusion. You would need https://pypi.org/project/pillow-jxl-plugin/ installed in the same environment of comfyui (depnds on how you installed it, I never use it, I just happen to pass here). Then in server.py and nodes.py , you would add on top: import pillow_jxl Then modify the code to save the images. The example given in the plugin are: import pillow_jxl from PIL import Image # Lossless encode a png image with Image.open("example.png") as img: img.save("example.jxl",lossless=True) You should use lossless=True for your use case. More on reddit.com
๐ŸŒ r/comfyui
22
2
May 16, 2024
Need to resize multiple images to a specific size in KB. How do I achieve this?
Assuming these are JPEGs, the amount of compression achieved at any particular quality level is highly dependent on the detail of the individual image. So I don't think it's possible to predict the size of the compressed image without actually doing the compression. So I think your best bet is to iterate through different quality levels for each image until you find one that gives you the output size you want. More on reddit.com
๐ŸŒ r/learnpython
15
3
May 22, 2023
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ compress png image in python using pil
Compress PNG image in Python using PIL - AskPython
April 22, 2023 - Using the library Pillow in Python, we can compress images in any format such as JPG, PNG, etc.
๐ŸŒ
The Python Code
thepythoncode.com โ€บ article โ€บ compress-images-in-python
How to Compress Images in Python - The Python Code
Learn how to reduce image file size by compressing and resizing the image using Pillow library in Python.
๐ŸŒ
Codeballistix
codeballistix.com โ€บ blog โ€บ python-pillow-image-lossless-compress
Using Python and Pillow to reduce an image's size on disk without sacrificing quality.
June 23, 2024 - Most of they heavy lifting is already done by python and pillow library. Thank the lord for open source contributors. We just need to call the right function, and give it the right parameters, which is 99% of programming if you think about it. Anyways, hereโ€™s the script below in its entirety. Its not long!! Its almost trivial. from PIL import Image import os source_image_path = input("Please enter the path to the image file you want to compress:") image = Image.open(source_image_path) size = image.size exif = image.getexif() final_photo_full = image.resize(size,resample = Image.Resampling.BILINEAR) target_path = input("Please enter the destination path:") final_photo_full.save(target_path,optimize=True,exif=exif)
Top answer
1 of 10
224

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.

2 of 10
31

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.

๐ŸŒ
PyTutorial
pytutorial.com โ€บ image-compression-and-optimization-with-pillow
PyTutorial | Image Compression and Optimization with Pillow
October 19, 2024 - Using Pillow in Python, you can easily compress and optimize images for better storage and performance. By adjusting quality settings, using optimization parameters, and creating thumbnails, you can significantly reduce file sizes without ...
Find elsewhere
๐ŸŒ
AbstractAPI
abstractapi.com โ€บ api guides, tips & tricks โ€บ how to compress images with python
Python Compress Image: How to Compress Images with Python (2023)
June 10, 2026 - The easiest way to lower image resolution in Python is to use Pillow. First, use the PIL Image object to open the image file. Next, write the image data to a new file, passing a quality option that lowers the quality of the final image.
๐ŸŒ
Thecodersblog
thecodersblog.com โ€บ home โ€บ programming โ€บ compress the image using python
Compress the image using python โ€“ The Coders Blog | Home
December 6, 2022 - If you use requirements.txt file to install the dependencies, then add the pillow library to the requirements.txt file. ... from PIL import Image image = Image.open('image.jpg') image.save('image_compressed.jpg', optimize=True, quality=50)
๐ŸŒ
KaijuConverter
kaijuconverter.com โ€บ home โ€บ guides โ€บ image compression and resizing with python and pillow
Image Compression & Resize with Python Pillow: Batch, WebP, Quality Control | KaijuConverter
May 7, 2026 - from PIL import Image import os def compress_jpg(input_path, output_path, quality=80): with Image.open(input_path) as img: if img.mode in ('RGBA', 'P', 'LA'): img = img.convert('RGB') img.save(output_path, format='JPEG', quality=quality, ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-compress-images-using-python-and-pil
How to compress images using Python and PIL? - GeeksforGeeks
July 15, 2025 - Assemble all the files in a folder and keep the file Compress.py in the same folder. Run the python file with python. ... # run this in any directory # add -v for verbose # get Pillow (fork of PIL) from # pip before running --> # pip install Pillow # import required libraries import os import sys from PIL import Image # define a function for # compressing an image def compressMe(file, verbose = False): # Get the path of the file filepath = os.path.join(os.getcwd(), file) # open the image picture = Image.open(filepath) # Save the picture with desired quality # To change the quality of image, #
๐ŸŒ
Flowygo
flowygo.com โ€บ home โ€บ pillow: optimize images with python
Pillow: optimize images with Python - Flowygo
October 7, 2022 - Before proceeding with image resizing, you need to install the Pillow library in your development environment. The fastest method is to use the following command: ... In the following code is an example of a function that allows you to compress all the images contained in a directory and save them in WEBP format in a specific path.
๐ŸŒ
GitHub
github.com โ€บ JRH89 โ€บ image-compression-and-resizing-with-python
GitHub - JRH89/image-compression-and-resizing-with-python: Recursive image compression and resizing with Python and Pillow ยท GitHub
Ensure that the input and output ... compress_images.py script if you need to adjust their paths. The script currently processes .jpg, .jpeg, .png, and .gif file formats. JPEG files are copied without additional compression, while other formats are converted to JPEG and compressed. If you encounter issues, make sure the Pillow library is ...
Author ย  JRH89
๐ŸŒ
Noerguerra
noerguerra.com โ€บ a-guide-to-efficient-image-compression-with-python
A guide to efficient image compression with Python โ€“ Noรฉ R. Guerra
January 11, 2022 - Define a new file name with _compressed appended at the end. Resize to max_size if the image is larger than max_size. Save with the new file name and 85% quality (if JPG) or 256 colors at most (if PNG). Pillow provides a simple way to perform complex operations on your images using Python.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Compress images in python with pillow module - YouTube
Blog post - https://nagasudhir.blogspot.com/2024/09/compress-photos-in-bulk-with-pillow.htmlIterating through files inside a folder using the glob module in ...
Published ย  October 5, 2024
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-compress-images-using-python-and-pil
How to Compress Images Using Python and PIL?
July 20, 2023 - In this article, we will look into the process of compressing images using Python and PIL. PIL, also known as the Python Imaging Library (now called Pillow), is a very useful and important library in Python that enables the opening, manipulation, and saving of various image file formats.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ how-to-compress-image-using-python-4f1deeb24bd9
How to Compress Images using Python | by Rajat upadhyaya | Python in Plain English
January 7, 2025 - How to Compress Images using Python We will see how we can compress our images using Python. For this, I am using the pillow library and also tkinter for selecting an image from the system. Letโ€™s โ€ฆ
๐ŸŒ
CodeForGeek
codeforgeek.com โ€บ home โ€บ compress png images in python using pillow (3 easy ways)
Compress PNG Images in Python Using Pillow (3 Easy Ways) | CodeForGeek
March 15, 2024 - Now that you have fallen in love with the image, letโ€™s try to fall in love with the methods of PNG compression. In this technique, we use the img.save() method from the Pillow library to save an image with specific compression settings.
๐ŸŒ
Bytescrum
blog.bytescrum.com โ€บ compressing-and-cropping-images-without-losing-quality-in-python
Compressing and Cropping Images Without Losing Quality in Python
September 13, 2024 - To compress and crop images without losing quality in Python, you can use the Pillow library.