["python","python-imaging-library","image-formats"]
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?
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