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 email@example.com. 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 program to determine if the given IPv4 Address is reserved using ipaddress module
- Python program to determine if the given IP Address is Public or Private using ipaddress module
- Python | type() function
- type and isinstance in Python
- Type Conversion in Python
- Type Hints in Python
- Python | Float type and its methods
- How to change any data type into a String in Python?
- Python - Test if all elements in list are of same type
- Python | Type conversion in dictionary values
- Python | Type conversion of dictionary items
- Python - Split heterogeneous type list
- Get the data type of column in Pandas - Python
- Python - Change type of key in Dictionary list
- Python - Pearson type-3 Distribution in Statistics
- Python | Type casting whole List and Matrix