- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Determine the type of an image using Python (imghdr)
The imghdr module in Python's standard library determines the type of image contained in a file or byte stream. There is only one function defined in imghdr module
imghdr.what(filename, h=None):
This function tests the image data contained in the file and returns a string describing the image type. The function also accepts h parameter. If given the filename is ignored and h is treated as the byte stream to test.
The imghdr module recognizes the following image types
value | image format |
---|---|
'rgb' | SGI ImgLib Files |
'gif' | GIF 87a and 89a Files |
'pbm' | Portable Bitmap Files |
pgm' | Portable Graymap Files |
'ppm' | Portable Pixmap Files |
'tiff' | TIFF Files |
'rast' | Sun Raster Files |
'xbm' | X Bitmap Files |
'jpeg' | JPEG data in JFIF or Exif formats |
'bmp' | BMP files |
'png' | Portable Network Graphics |
'webp' | WebP files |
'exr' | OpenEXR Files |
Example
>>> import imghdr >>> imghdr.what('bass.gif') 'gif' >>> imghdr.what('polar.png') 'png' >>> imghdr.what('test.jpg') 'jpeg'
- Related Articles
- Determine the type of an image in Python?
- Determine type of sound file using Python (sndhdr)
- How to identify the type of an Image instance using FabricJS?
- Python Program to detect the edges of an image using OpenCV
- How to extract the foreground of an image using OpenCV Python?
- An object 20 cm from a spherical mirror gives rise to a virtual image 15 cm behind the mirror. Determine the magnification of the image and the type of mirror used.
- Cartooning an Image using OpenCV in Python?
- Reading an image using Python OpenCv module
- How to find the Fourier Transform of an image using OpenCV Python?
- Determine common type following standard coercion rules in Python
- How to compute the aspect ratio of an object in an image using OpenCV Python?
- How to compute the morphological gradient of an image using OpenCV in Python?
- How to compute the extent of an object in image using OpenCV Python?
- How to convert an RGB image to HSV image using OpenCV Python?
- Determine if the type in the first argument is a subclass of second in Python

Advertisements