Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the format of image with PIL?

After loading an image file with PIL.Image, how can I determine whether the image file is a PNG/JPG/BMP/GIF? I understand very little about these file formats, can PIL get the format metadata from the file header? Or does it need to 'analyze' the data within the file?

If PIL doesn't provide such an API, is there any python library that does?

like image 748
NeoWang Avatar asked Sep 20 '15 12:09

NeoWang


People also ask

What is PIL image format?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.

Does PIL support JPEG?

Pillow supports reading JPEG Compressed or raw BLP1 images, and all types of BLP2 images.


1 Answers

Try:

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

like image 107
Mikko Ohtamaa Avatar answered Sep 26 '22 04:09

Mikko Ohtamaa