Suppose you are given an image type file and you need to determine the type of that file. In simple words, you need to get the extension of that image type file. This can be used in a project to verify whether the image you have requested for is actually an image and with which extension does it come.
Use following command for Installation:
npm install imghdr
The imghdr module determines the type of image contained in a file or byte stream. The imghdr module defines the following function:
Tests the image data contained in the file named by filename, and returns a string describing the image type. If optional h is provided, the filename is ignored and h is assumed to contain the byte stream to test.
Note: The path of the file needs to be correct with its correct name. If the image file and the code file are in the same directory, you don’t need to specify the whole path. Just pass the name of the file as I did in the next example
The extensions that can be recognized in module are-‘rgb’, ‘gif’,’ pbm’,’ pgm’,’ ppm’,’ tiff’, ‘rast’, ‘xbm’, ‘jpeg’, ‘bmp’, ‘png’, ‘webp’, ‘exr’. In Python 3.5, extensions ‘exr’ and ‘webp’ are also added.
|‘rgb’||SGI ImgLib Files|
|‘gif’||GIF 87a and 89a Files|
|‘pbm’||Portable Bitmap Files|
|‘pgm’||Portable Graymap Files|
|‘ppm’||Pportable Pixmap Files|
|‘rast’||Sun Raster Files|
|‘xbm’||X Bitmap Files|
|‘jpeg’||JPEG data in JFIF or Exif formats|
|‘png’||Portable Network Graphics|
Input : picture.gif Output : gif Input: picture.jpeg Output : jpeg
This is a simple program but used to solve big problems in real life projects.
This article is contributed by Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python | Working with the Image Data Type in pillow
- Getting started with Scikit-image: image processing in Python
- Converting an image to ASCII image in Python
- Python | Ways to determine common prefix in set of strings
- Python PIL | Image.tell()
- Type Hints in Python
- type and isinstance in Python
- Python | type() function
- Type Conversion in Python
- Python PIL | Image.new() method
- Python PIL | Image.getdata()
- Image Captioning using Python
- Python PIL | Image.histogram()
- Python | Float type and its methods
- Image Registration using OpenCV | Python