imghdr — Determine the type of an image¶
Source code:Lib/imghdr.py
Deprecated since version 3.11, will be removed in version 3.13:Theimghdr module is deprecated(seePEP 594 for details and alternatives).
Theimghdr module determines the type of image contained in a file orbyte stream.
Theimghdr module defines the following function:
- imghdr.what(file,h=None)¶
Tests the image data contained in the file named byfile, and returns astring describing the image type. If optionalh is provided, thefileargument is ignored andh is assumed to contain the byte stream to test.
Changed in version 3.6:Accepts apath-like object.
The following image types are recognized, as listed below with the return valuefromwhat():
Value | Image format |
|---|---|
| SGI ImgLib Files |
| GIF 87a and 89a Files |
| Portable Bitmap Files |
| Portable Graymap Files |
| Portable Pixmap Files |
| TIFF Files |
| Sun Raster Files |
| X Bitmap Files |
| JPEG data in JFIF or Exif formats |
| BMP files |
| Portable Network Graphics |
| WebP files |
| OpenEXR Files |
New in version 3.5:Theexr andwebp formats were added.
You can extend the list of file typesimghdr can recognize by appendingto this variable:
- imghdr.tests¶
A list of functions performing the individual tests. Each function takes twoarguments: the byte-stream and an open file-like object. When
what()iscalled with a byte-stream, the file-like object will beNone.The test function should return a string describing the image type if the testsucceeded, or
Noneif it failed.
Example:
>>>importimghdr>>>imghdr.what('bass.gif')'gif'