I see two options which you can both achieve using PIL library given for instance an RGB image.

1 – Reduce the image resolution (and therefore quality – you might not want that). Use the Image.thumbnail() or Image.resize() method provided by PIL.

2 – Reduce the color range of the image (the number of colors will affect file size accordingly). This is what many online converters will do.

img_path = "img.png"
img = Image.open(img_path)
img = img.convert("P", palette=Image.ADAPTIVE, colors=256)
img.save("comp.png", optimize=True)
Answer from Joe Web on Stack Overflow
🌐
AskPython
askpython.com › home › compress png image in python using pil
Compress PNG image in Python using PIL - AskPython
April 22, 2023 - To compress images in Python, use the Pillow library. Install it with pip install pillow, then create a function to resize and save the image with optimized quality. Call this function with your image file to get a compressed version, allowing ...
🌐
Aspose
products.aspose.com › aspose.imaging › python via .net › compress › png
PNG Images Compression with Python | products.aspose.com
July 24, 2025 - Achieving a balance between file size and image quality requires a careful selection of algorithms and compression levels. To compress images in PNG format, we’ll employ Aspose.Imaging for Python via .NET API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Python platform.
🌐
PyPI
pypi.org › project › optimize-images
optimize-images
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Aspose
blog.aspose.com › aspose.blogs › compress images in python
Compress Images in Python | Compress PNG, JPG and TIF
July 7, 2023 - You can either download the library or install it using the following command. ... To compress an image, you can set the value of the compression level from 0 to 9. The value 9 represents the maximum compression and 0 is the store mode. The following are the steps to compress a PNG image in Python.
🌐
Medium
medium.com › @omcar17295 › how-to-compress-images-using-python-2bfae115dca1
How to compress images using Python | by Omkar Mohan Joshi | Medium
January 1, 2023 - # Loop through all the images in the input directory for filename in os.listdir(input_dir): # Check if the file is an image if filename.endswith('.JPG') or filename.endswith('.png'): # Open the image image = Image.open(f'{input_dir}/{filename}') # Compress the image and save it to the output directory image.save(f'{output_dir}/{filename}', quality=quality) print('Done!')
🌐
TinyPNG
tinify.com › developers › reference › python
TinyPNG – API Reference
Python · Java · .NET · The Tinify API allows you to compress and optimize AVIF, WebP, JPEG and PNG images. It is designed as a REST service. This page describes the raw HTTP API. We also maintain client libraries in various languages that make it very easy to interact with the Tinify API.
🌐
The Python Code
thepythoncode.com › article › compress-images-in-python
How to Compress Images in Python - The Python Code
The image size is reduced to 379.25KB from 425.65KB, which is about an 11% reduction. Next, let's try to pass -j for converting from PNG to JPEG: $ python compress_image.py sample-satellite-images.png -j
Find elsewhere
🌐
GitHub
github.com › hhoy › python-zopflipng
GitHub - hhoy/python-zopflipng: Lossless PNG compressor - Use the zopfli compression algorithm,It works in python
# a simple example, using the default configuration from zopflipng import png_optimize data = open('test.png', 'rb').read() result, code = png_optimize(data) # if code ==0 ,png compression success if code == 0: # save png with open('result.png','wb') as f: f.write(result) f.close()
Author   hhoy
🌐
Transloadit
transloadit.com › devtips › efficient-png-optimization-in-python-with-oxipng
Efficient PNG optimization in Python with Oxipng
May 12, 2025 - PNG files are lossless and high-quality, yet they often ship with excess metadata and sub-optimal compression. In this DevTip, we’ll show how to shrink PNGs from Python by calling the modern, Rust-powered Oxipng engine through its actively maintained Python binding, pyoxipng.
🌐
GitConnected
levelup.gitconnected.com › compress-images-with-16-lines-of-python-code-and-tinypng-api-6ff33861cfc5
Compress images with 16 lines of python code and TinyPNG API | by 0𝕏koji | Level Up Coding
September 17, 2021 - with open(“unoptimized.jpg”, ‘rb’) as source: source_data = source.read() result_data = tinify.from_buffer(source_data).to_buffer() source = tinify.from_url(“https://tinypng.com/images/panda-happy.png") source.to_file(“optimized.png”)app.py import tinify from glob import glob import os.path tinify.key = “your_api_key” source_dir_name = ‘src’ destination_dir_name = ‘dist’ # get all files names in directory files = glob(source_dir_name + ‘/*’) # compress all files for file in files: print(‘compressing ‘ + file) source = tinify.from_file(file) file_name, ext = os
🌐
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.
🌐
GitHub
gist.github.com › f0d66fd4162d2cceb27a
Simple python code to compress (jpeg/png) images uploaded to S3. In this use case I want to compress down all images that are over 500 KB down to 80% quality which brings the images down to about 100KB. Feel free to muck with the values. · GitHub
Simple python code to compress (jpeg/png) images uploaded to S3. In this use case I want to compress down all images that are over 500 KB down to 80% quality which brings the images down to about 100KB. Feel free to muck with the values. - compress_s3_images.py
🌐
Understanding Data
understandingdata.com › posts › how-to-easily-resize-compress-your-images-in-python
How To Easily Resize & Compress Your Images In Python
base_width = 360 image = Image.open('example-image.jpg') width_percent = (base_width / float(image.size[0])) hsize = int((float(image.size[1]) * float(width_percent))) image = image.resize((base_width, hsize), PIL.Image.ANTIALIAS) image.save('resized_compressed_image.jpg')
🌐
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 - Hello and welcome to the world of image optimization! In this quick guide, we will take a look at compressing PNG images using Pillow in Python. We will learn the basics of image compression and see how it makes a big difference in improving web development and saving storage space.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-compress-images-using-python-and-pil
How to compress images using Python and PIL? - GeeksforGeeks
July 15, 2025 - Till Windows 7, Microsoft used to give MS Office Picture Manager which could be used to compress images till an extent, but it also had some limitations. Those who know a bit of python can install python and use pip install pillow in command prompt(terminal for Linux users) to install pillow fork.
🌐
Understanding Data
understandingdata.com › posts › how-to-compress-multiple-images-in-python
How To Compress Multiple Images In Python - Just Understanding Data
There many libraries that allow you to easily optimise images with Python: Pillow – This library builds on top of PIL and can be used for the following image formats: PNG, PPM, JPEG, GIF, BMP and TIFF. img4web – This script optimises .jpg and .png images for the web, after running it you’ll receive lossless compression for the images.
🌐
Uploadcare
uploadcare.com › web performance category › python libraries to compress & resize images fast
Python libraries to compress & resize images fast
August 29, 2025 - Img4web is a Python script that optimizes JPEG, PNG, and animated GIFs for your website. The script can produce lossless and slightly compressed images, which nonetheless reduces the loading time of pages with pictures.
🌐
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 - Next, we used AbstractAPI's Free Image Processing endpoint to perform lossy compression on a hosted image file. The easiest way to lower image resolution in Python is to use Pillow. First, use the PIL Image object to open the image file.
🌐
DEV Community
dev.to › 0xkoji › compress-images-with-python-16-lines-and-tinypng-api-3l69
Compress Images with Python(16 lines) and TinyPNG API - DEV Community
September 16, 2021 - import tinify from glob import glob import os.path tinify.key = "your_api_key" source_dir_name = 'src' destination_dir_name = 'dist' # get all files names in directory files = glob(source_dir_name + '/*') # compress all files for file in files: print('compressing ' + file) source = tinify.from_file(file) file_name, ext = os.path.splitext(file) file_name = file_name.replace(source_dir_name + '/', '') source.to_file(destination_dir_name + "/" + file_name + ".png") print('compressed all images') Before running the script, we need 2 small things. First, create two directories(src and dist). If you