Change language

Determine image type in Python using imghdr

| |

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.

imghdr module

Use the following command to install:

 npm install imghdr 

Description:
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.

> ’rast’ td> ’png’
Value Image Format
’rgb’ SGI ImgLib Files
’gif’
’pbm’ Portable Bitmap Files
’pgm’ tr>
’ppm’ Pportable Pixmap Files
’tiff’ IFF Files
Sun Raster Files
’xbm’ X Bitmap Files
’jpeg’ JPEG data in JFIF or Exif formats
’bmp’ BMP Files
Portable Network Graphics
’webp’ WebP Files
’exr’ OpenEXR Files

Example:

 Input : picture.gif Output: gif Input: picture.jpeg Output: jpeg 

import imghdr

x = imghdr.what ( "picture.gif"

# path to the image in as a parameter

 

prin t (x)

Output:

 gif 

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically