Determine the type of an image in Python?

PythonServer Side ProgrammingProgramming

In this section we are going to see the type of image file we have. So consider a situation, where in a directory we have hundreds of image file and we want to get all the jgeg(or any particular image file type) file type. All this we are going to do programmatically using python.

Python provide library to determine the type of an image, on such library is imghdr.

The python imghdr package determines the type of image contained in a file or byte stream.

Installation

There is a very much chances that if you are using python 3.6 or higher, imghdr module is an standard package and will come with python installation.

To install imghdr in your machine, just run below command in your command terminal:

pip install imghdr

After successful installation, to verify imghdr is installed properly or not just import the module in your python shell.

>>> import imghdr
>>>

In case you are getting no error, it means imghdr is installed in your machine.

Syntax

The imghdr package defines the following function:

imghdr.what(filename[, h])

Where

  • Filename: tests the image data contained in the file named by filename and returns a string describing the image type.

  • h: Its optional, in case h is there- then the filename is ignored and h is assumed to contain the byte stream to test.

Below are the permitted image types which are recognized using imghdr package.

Value
Image format
'rgb'
SGI ImgLib Files
'gif'
GIF 87a and 89a Files
'pbm'
Portable Bitmap Files
'pgm'
Portable Graymap Files
'ppm'
Portable Pixmap Files
'tiff'
TIFF Files
'rast'
Sun Raster Files
'xbm'
X Bitmap Files
'jpeg'
JPEG data in JFIF or Exif formats
'bmp'
BMP files
'png'
Portable Network Graphics

But we can extend the list of file types which imghdr package can recognise by appending to this variable.

imghdr.tests

This function contains list of functions performing the individual tests. Each function takes two arguments: the byte-stream and an open file-like object. However, when what() is called with a byte-stream, the file-like object will be None.

The test function will return the image type as a string else None if it failed.

>>> import imghdr
>>> imghdr.what('clock.jpg')
'jpeg'

Below is just one implementation of imghdr package, where if some particular imagefile extension is there do particular operation:

def identify_filetype(url, imageName, folderName):
   session = _setupSession()
   try:
      # time out is another parameter tuned
      image = session.get(url, timeout = 5)
      with open(os.path.join(folderName, imageName),'wb') as fout:
      fout.write(image.content)
      fileExtension = imghdr.what(os.path.join(folderName, imageName))
      if fileExtension is None:
         os.remove(os.path.join(folderName, imageName))
      else:
         newName = imageName + '.' + str(fileExtension)
         os.rename(os.path.join(folderName, imageName), os.path.join(folderName, newName))
except Exception as e:
print ("failed to download one pages with url of " + str(url))
raja
Published on 08-Apr-2019 13:09:07
Advertisements