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 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 | Working with the Image Data Type in pillow
- Primitive data type vs. Object data type in Java with Examples
- Python | Ways to determine common prefix in set of strings
- Converting an image to ASCII image in Python
- Getting started with Scikit-image: image processing in Python
- Determine the first and last iteration in a foreach loop in PHP?
- ML | Determine the optimal value of K in K-Means Clustering
- Silhouette Algorithm to determine the optimal value of k
- How to determine Period Range with Frequency in Pandas?
- Mahotas - Labelled Image from the Normal Image
- PyQt5 – How to get cropped square image from rectangular image ?
- PyQt5 – How to create circular image from any image ?
- Mahotas - Reconstructing image from transformed Daubechies wavelet image
- Multi-Label Image Classification - Prediction of image labels
- Image resizing using Seam carving using OpenCV in Python
- Python Tkinter | Create different type of lines using Canvas class
- Cartooning an Image using OpenCV - Python
- OpenCV Python Program to analyze an image using Histogram