Try:

from PIL import Image

img = Image.open(filename)
print(img.format)  # 'JPEG'

More info

  • https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format

  • https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html

Answer from Mikko Ohtamaa on Stack Overflow
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › handbook › image-file-formats.html
Image file formats - Pillow (PIL Fork) 12.2.0 documentation
Added in version 10.1.0: BC5U can be read in RGB mode, and 8-bit color indexed images can be read in P mode. Added in version 11.2.1: DXT1, DXT3, DXT5, BC2, BC3 and BC5 pixel formats can be saved:: ... Pillow reads and writes DIB files.
🌐
Readthedocs
pillow-wiredfool.readthedocs.io › en › latest › handbook › image-file-formats.html
Image file formats — Pillow v2.4.0 (PIL fork) - Read the Docs
The open() function identifies files from their contents, not their names, but the save() method looks at the name to determine which format to use, unless the format is given explicitly. PIL reads and writes Windows and OS/2 BMP files containing 1, L, P, or RGB data.

Try:

from PIL import Image

img = Image.open(filename)
print(img.format)  # 'JPEG'

More info

  • https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format

  • https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html

Answer from Mikko Ohtamaa on Stack Overflow
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.2.0 documentation
If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used. ... Extra parameters to the image writer. These can also be set on the image itself through encoderinfo. This is useful when saving multiple images: # Saving XMP data to a single image from PIL import Image red = Image.new("RGB", (1, 1), "#f00") red.save("out.mpo", xmp=b"test") # Saving XMP data to the second frame of an image from PIL import Image black = Image.new("RGB", (1, 1)) red = Image.new("RGB", (1, 1), "#f00") red.encoderinfo = {"xmp": b"test"} black.save("out.mpo", save_all=True, append_images=[red])
🌐
Wikipedia
en.wikipedia.org › wiki › Python_Imaging_Library
Python Imaging Library - Wikipedia
1 week ago - PIL offers several standard procedures for image manipulation. These include: ... Supported file formats include PPM, PNG, JPEG, GIF, TIFF, and BMP.
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_converting_image_file_formats.htm
Python Pillow - Converting Image Formats
This example converts the image from JPEG to PNG format. from PIL import Image # Open the source image in JPEG format image = Image.open("Images/logo.jpg") # Convert and save the image in PNG format image.save("output_image_PNG_format.png") ...
🌐
GitHub
github.com › python-pillow › Pillow › blob › main › docs › handbook › image-file-formats.rst
Pillow/docs/handbook/image-file-formats.rst at main · python-pillow/Pillow
The Python Imaging Library supports a wide variety of raster file formats. Over 30 different file formats can be identified and read by the library. Write support is less extensive, but most common interchange and presentation formats are supported. The :py:meth:`~PIL.Image.open` function identifies files from their contents, not their names, but the :py:meth:`~PIL.Image.Image.save` method looks at the name to determine which format to use, unless the format is given explicitly.
Author   python-pillow
Find elsewhere
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › handbook › concepts.html
Concepts - Pillow (PIL Fork) 12.2.0 documentation
>>> from PIL import Image >>> im = Image.new("RGBA", (1, 1), 0x04030201) >>> im.getpixel((0, 0)) (1, 2, 3, 4) Some methods accept other forms, such as color names. See Color names. You can attach auxiliary information to an image using the info attribute. This is a dictionary object. How such information is handled when loading and saving image files is up to the file format handler (see the chapter on Image file formats).
🌐
Marg ERP
margcompusoft.com › m › pil-format
Understanding the PIL Format: A Powerful Tool for Image Processing - Marg ERP Blog
June 1, 2023 - It allows for image-based data ... visualizations that incorporate images. The PIL format is a powerful and versatile library that simplifies image processing tasks in Python....
🌐
Neptune.ai
neptune.ai › blog › pil-image-tutorial-for-machine-learning
Essential Pil (Pillow) Image Tutorial (For Machine Learning ...
OpenAI is acquiring Neptune to deepen visibility into model behavior and strengthen the tools researchers use to track experiments and monitor training.
🌐
pytz
pythonhosted.org › pyglet › programming_guide › supported_image_formats.html
Supported image formats
See the Python Imaging Library Handbook for a list of such formats. The only supported save format is PNG, unless PIL is installed, in which case any format it supports can be written.
🌐
Pillow
pillow.readthedocs.io › en › latest › reference › Image.html
Image module - Pillow (PIL Fork) 12.3.0.dev0 documentation
If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used. ... Extra parameters to the image writer. These can also be set on the image itself through encoderinfo. This is useful when saving multiple images: # Saving XMP data to a single image from PIL import Image red = Image.new("RGB", (1, 1), "#f00") red.save("out.mpo", xmp=b"test") # Saving XMP data to the second frame of an image from PIL import Image black = Image.new("RGB", (1, 1)) red = Image.new("RGB", (1, 1), "#f00") red.encoderinfo = {"xmp": b"test"} black.save("out.mpo", save_all=True, append_images=[red])
🌐
TutorialsPoint
tutorialspoint.com › working-with-the-image-data-type-in-python-pillow
Working with the Image Data Type in Python Pillow
August 3, 2023 - Image Processing Capabilities − Pillow provides a comprehensive set of tools for image manipulation such as opening, editing, enhancing and saving images. It supports various image formats for making it versatile for handling different types of image data.
🌐
Real Python
realpython.com › image-processing-with-the-python-pillow-library
Image Processing With the Python Pillow Library – Real Python
January 8, 2025 - Python Pillow allows you to manipulate images and perform basic image processing tasks. As a fork of the Python Imaging Library (PIL), Pillow supports image formats like JPEG, PNG, and more, enabling you to read, edit, and save images.
🌐
GitHub
github.com › python-pillow › Pillow › blob › main › docs › handbook › tutorial.rst
Pillow/docs/handbook/tutorial.rst at main · python-pillow/Pillow
When you open a file, the file header is read to determine the file format and extract things like mode, size, and other properties required to decode the file, but the rest of the file is not processed until later. This means that opening an image file is a fast operation, which is independent of the file size and compression type. Here’s a simple script to quickly identify a set of image files: import sys from PIL import Image for infile in sys.argv[1:]: try: with Image.open(infile) as im: print(infile, im.format, f"{im.size}x{im.mode}") except OSError: pass
Author   python-pillow
🌐
Finxter
blog.finxter.com › pillow-to-convert-image-formats-png-jpg-and-more
Pillow to Convert Image Formats Between .png and .jpg – and Work With Images – Be on the Right Side of Change
But be careful, because according to the official Pillow web, I can’t have both installed in your system at the same time. This library supports many formats such as BMP, DDS, DIB, EPS, GIF, ICNS, ICO, I.M., JPEG, JPEG 2000, MSP, PCX, PNG, PPM, SGI, SPIDER, TGA, TIFF, and many more.
🌐
Topcoder
topcoder.com › thrive › articles › image-processing-in-python-using-pillow
Image Processing in Python Using Pillow
May 18, 2022 - It ought to give a strong groundwork to general image processing tools. Pillow supports a range of image file formats: PNG, JPEG, PPM, GIF, TIFF, and BMP.
🌐
GeeksforGeeks
geeksforgeeks.org › python-pil-image-save-method
Python PIL | Image.save() method - GeeksforGeeks
August 2, 2024 - Use the format option to solve this. IOError – If the file could not be written. The file may have been created, and may contain partial data. ... # Importing Image module from PIL package from PIL import Image import PIL # creating a image object (main image) im1 = Image.open(r"C:\Users...
🌐
Readthedocs
pillow-wiredfool.readthedocs.io › en › stable › handbook › image-file-formats.html
Image file formats — Pillow v2.7.0 (PIL fork)
The open() function identifies files from their contents, not their names, but the save() method looks at the name to determine which format to use, unless the format is given explicitly. PIL reads and writes Windows and OS/2 BMP files containing 1, L, P, or RGB data.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pil-image-open-method
Python PIL | Image.open() method - GeeksforGeeks
October 7, 2025 - To begin using Pillow, it must first be installed through the Python package manager: ... Returns: An Image object and Raises IOError if the file cannot be opened. Note: This is a lazy operation. The image is identified but not fully read until you access its data. Image.open() is used to load an image file as an Image object for viewing or further processing. ... This example saves the image as a PNG file regardless of its original format.