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
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
Beware! If the image you are trying to get the format from an image that was created from a copy(), the format will be None.
From the docs
Copies of the image will contain data loaded from the file, but not the file itself, meaning that it can no longer be considered to be in the original format. So if
copy()is called on an image, or another method internally creates a copy of the image, then any methods or attributes specific to the format will no longer be present. Thefp(file pointer) attribute will no longer be present, and theformatattribute will beNone.
Methods that create an implicit copy include:
convert()crop()remap_palette()resize()reduce()rotate()split()- these were all I found as of today, but others may be added
For example:
img = Image.open(filename)
print(img.format) # 'JPEG'
img2 = img.copy()
print(img2.format) # None
print(img.rotate(90, expand=True).format). # None
So if you need to get the format from a copy, you need to look to the original image.
Videos
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