Suppose you are given an image type file and you need to determine the type of this file. Simply put, you need to get the extension of that image type file. This can be used in a project to check if the image you requested is actually an image and what extension it comes with.
Use the following command to install:
npm install imghdr
The imghdr module determines the type of image contained in file or stream of bytes. The imghdr module defines the following function:
imghdr.what (filename [, h])
Checks the image data contained in the file filename and returns a string describing the image type. If the optional h is specified, the filename is ignored and h is assumed to contain the byte stream to check.
Note. The file path must be correct with the correct name. If the image file and the code file are in the same directory, you don`t need to include the entire path. Just pass the filename as I did in the following example
Extensions that can be recognized in the module: -rgb, gif, pbm, pgm, ppm, tiff, rast, xbm, jpeg, bmp, & # 39; png & # 39 ;, & # 39; webp & # 39 ;, & # 39; exr & # 39 ;. Python 3.5 also adds the “exr” and “webp” extensions.
|`rgb`||SGI ImgLib Files|
|`pbm`||Portable Bitmap Files|
|`ppm`||Pportable Pixmap Files|
|Sun Raster Files|
|`xbm`||X Bitmap Files|
|`jpeg`||JPEG data in JFIF or Exif formats|
|Portable Network Graphics|
Input : picture.gif Output: gif Input: picture.jpeg Output: jpeg
This is a simple program, but it is used for big problems in real projects.
This article is provided by Rishabh Bansal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.