Change language

Determine image type in Python using imghdr

| |

Suppose you are given an image type file and you need to determine the type of this file. Simply put, you need to get the extension of that image type file. This can be used in a project to check if the image you requested is actually an image and what extension it comes with.

imghdr module

Use the following command to install:

 npm install imghdr 

The imghdr module determines the type of image contained in file or stream of bytes. The imghdr module defines the following function:

 imghdr.what (filename [, h]) 

Checks the image data contained in the file filename and returns a string describing the image type. If the optional h is specified, the filename is ignored and h is assumed to contain the byte stream to check.

Note. The file path must be correct with the correct name. If the image file and the code file are in the same directory, you don’t need to include the entire path. Just pass the filename as I did in the following example

Extensions that can be recognized in the module: -rgb, gif, pbm, pgm, ppm, tiff, rast, xbm, jpeg, bmp, & # 39; png & # 39 ;, & # 39; webp & # 39 ;, & # 39; exr & # 39 ;. Python 3.5 also adds the "exr" and "webp" extensions.

> ’rast’ td> ’png’
Value Image Format
’rgb’ SGI ImgLib Files
’pbm’ Portable Bitmap Files
’pgm’ tr>
’ppm’ Pportable Pixmap Files
’tiff’ IFF Files
Sun Raster Files
’xbm’ X Bitmap Files
’jpeg’ JPEG data in JFIF or Exif formats
’bmp’ BMP Files
Portable Network Graphics
’webp’ WebP Files
’exr’ OpenEXR Files


 Input : picture.gif Output: gif Input: picture.jpeg Output: jpeg 

import imghdr

x = imghdr.what ( "picture.gif"

# path to the image in as a parameter


prin t (x)



This is a simple program, but it is used for big problems in real projects.

This article is provided by Rishabh Bansal . If you are as Python.Engineering and would like to contribute, you can also write an article using or by posting an article contribute @ See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method