Save the image with 100% quality. This will still save the image slightly different from the original, because it will also use subsampling to reduce the image size.

img.save('test.jpg', quality=100)

You can also turn off subsampling to make sure you get the exact same as the original image.

img.save('test.jpg', quality=100, subsampling=0)
Answer from Ali Sajjad Rizavi on Stack Overflow
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › handbook › image-file-formats.html
Image file formats - Pillow (PIL Fork) 12.3.0 documentation
... ZLIB compression level, a number between 0 and 9: 1 gives best speed, 9 gives best compression, 0 gives no compression at all. Default is 6. When optimize option is True compress_level has no effect (it is set to 9 regardless of a value passed).
Discussions

python PIL save image different size original - Stack Overflow
I´m working on a project with PIL in python. Simply by opening and saving an image makes the output image bigger (in Bytes) than the original, maintaining the same resolution, and i don´t know why.... More on stackoverflow.com
🌐 stackoverflow.com
Saving PNG images with PIL is 4 times slower than saving them with OpenCV
The produced images are slightly ... compression being used. Here are the two (black) images I obtained tmp1.png (PIL-image, 33KB) tmp2.png (OpenCV-image, 38KB) ... Is there some way how I can speed up PIL to match the performance of OpenCV in this scenario (other than converting to a numpy array and using OpenCV), e.g., by providing extra parameters to the save ... More on github.com
🌐 github.com
36
January 25, 2022
selenium - Compress PNG image in Python using PIL - Stack Overflow
The file size is about 3.5 MB since it is a long, scrollable page and I need the full browser screenshot. I need a way to compress the saved screenshots, or save them as smaller file size PNG images so that I can attach and send several such screenshots in the same email using another Python ... More on stackoverflow.com
🌐 stackoverflow.com
python - Is there a way to speed up the Save method with PIL? - Stack Overflow
Save this answer. ... Show activity on this post. Compressing image data to PNG takes time - CPU time. There might be a better performant lib to that than PIL, but you'd have to interface it with Python, and it still would take sometime. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Readthedocs
pc-pillow.readthedocs.io › en › latest › Image_class › Image_save.html
5. Image save — PC-Pillow
from PIL import Image with ... as a jpg. compress_level is a number between 0 and 9: 1 gives best speed, 9 gives best compression, 0 gives no compression at all....
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.3.0 documentation
This method returns raw image data derived from Pillow’s internal storage. For compressed image data (e.g. PNG, JPEG) use save(), with a BytesIO parameter for in-memory data.
🌐
GitHub
github.com › python-pillow › Pillow › issues › 5986
Saving PNG images with PIL is 4 times slower than saving them with OpenCV · Issue #5986 · python-pillow/Pillow
January 25, 2022 - The produced images are slightly different in file-size so potentially there is a more sophisticated compression being used. Here are the two (black) images I obtained tmp1.png (PIL-image, 33KB) tmp2.png (OpenCV-image, 38KB) ... Is there some way how I can speed up PIL to match the performance of OpenCV in this scenario (other than converting to a numpy array and using OpenCV), e.g., by providing extra parameters to the save method?
Author   python-pillow
🌐
Python Examples
pythonexamples.org › python-pillow-save-image-as-png
Python Pillow - Save image as PNG
Since this example is for only demonstrating how to save an image as a PNG image, we are neither modifying the image not applying any transformations on it. But, in realtime applications, you might be doing additional operations on the image. Or, if you are just converting your images into PNG format, then this is a perfect example. from PIL import Image # Open the input image image = Image.open('input_image.jpg') # Save the image as a PNG file image.save('output_image.png', format='PNG') # Close the image image.close()
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pil-image-save-method
Python PIL | Image.save() method - GeeksforGeeks
December 23, 2025 - Explanation: img.save("eyes2.png") automatically switches the image format to PNG based on the extension. Example 2: This example saves the image with a lower JPEG quality to reduce file size. ... A new file compressed.jpg with reduced quality. Explanation: quality=40 tells the JPEG writer to compress the image more, producing a smaller file. Example 3: Saving an Image Using an Explicit Format when using a file object instead of a filename, the format must be explicitly provided. ... from PIL import Image img = Image.open("flower.jpg") with open("saved_image.bin", "wb") as f: img.save(f, format="JPEG")
🌐
GitHub
github.com › sirfz › tesserocr › issues › 207
Quality degradation due to PIL.Image.save · Issue #207 · sirfz/tesserocr
December 18, 2019 - There's a big difference for JPEG files between CLI and API segmentation results. That difference vanishes when using file I/O. I have been trying to arbitrarily set the format attribute of the PIL.Image object I am passing to SetImage to different formats, but it does not help.
Author   sirfz
🌐
GitHub
github.com › python-pillow › Pillow › issues › 1211
Saving a PNG file is slow · Issue #1211 · python-pillow/Pillow
May 4, 2015 - Hello, I wan to create a PNG file from the data with dimensions 4928x3264. It takes roughly 9 seconds to save this data to a file. I am looking into improving this time for my work. Can you give a ...
Author   python-pillow
🌐
GitHub
github.com › invoke-ai › InvokeAI › issues › 4786
[enhancement]: expose `PIL.image.save()`'s `compress_level` arg · Issue #4786 · invoke-ai/InvokeAI
October 4, 2023 - 9 is the highest, resulting in the smallest files possible, but it takes the most time. 0 is no compression. When working with really large images, using compression can really take a long time - like ...
Author   invoke-ai
🌐
Flowygo
flowygo.com › home › pillow: optimize images with python
Pillow: optimize images with Python - Flowygo
October 7, 2022 - Optimizing images is key to making websites faster and improving SEO. With the advent of WebP format, it is possible to provide quality images but much "lighter". In this article we discover how to transform jpg and/or png images into the webp format using a few lines of code written in Python and the Pillow library.
🌐
AskPython
askpython.com › home › compress png image in python using pil
Compress PNG image in Python using PIL - AskPython
April 22, 2023 - In this article, we have learned how to compress images in Python using one of the image processing, open-source libraries called Pillow. PIL or pillow is one of the most powerful image manipulating modules in Python. It is most commonly used for reducing image size, implicitly or explicitly converting image from one format to another, save images, compressing images and much more.
🌐
Reddit
reddit.com › r/learnpython › how to transform pil object to png file?
r/learnpython on Reddit: How to transform PIL object to PNG file?
April 24, 2021 -

I have a database that stores some information and one of those values has to be an image of a chart I create with matplotlib. I know you can save charts as png files but that would save to my computer and I'd like for it to go directly to the database (SQLite3). I did a bit of research and found you could transform a matplotlib chart to a PIL image using this function:

def fig2img(fig):
    """Convert a Matplotlib figure to a PIL Image and return it"""
    import io
    buf = io.BytesIO()
    fig.savefig(buf)
    buf.seek(0)
    img = Image.open(buf)
    return img

Now that I have the chart transformed to a PIL object, if I print the type of the image I get this:

img = fig2img(fig)

print(type(img))
# <class 'PIL.PngImagePlugin.PngImageFile'>

Now, my question is, how can I transform this data format to a raw PNG file without saving it to my hard drive so I can later plug it into the database?

🌐
GeeksforGeeks
geeksforgeeks.org › create-transparent-png-image-with-python-pillow
Create transparent png image with Python - Pillow - GeeksforGeeks
February 24, 2021 - Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aids in editing, creating and saving images. Support for Python Imaging Library got discontinued in 2011, but a project named pillow forked