- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Determine the type of an image in Python?
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.
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.
The imghdr package defines the following function:
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.
|'rgb'||SGI ImgLib Files|
|'gif'||GIF 87a and 89a Files|
|'pbm'||Portable Bitmap Files|
|'pgm'||Portable Graymap Files|
|'ppm'||Portable Pixmap Files|
|'rast'||Sun Raster Files|
|'xbm'||X Bitmap Files|
|'jpeg'||JPEG data in JFIF or Exif formats|
|'png'||Portable Network Graphics|
But we can extend the list of file types which imghdr package can recognise by appending to this variable.
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))
- Determine the type of an image using Python (imghdr)
- Determine type of sound file using Python (sndhdr)
- Determine common type following standard coercion rules in Python
- Determine if the type in the first argument is a subclass of second in Python
- An object 20 cm from a spherical mirror gives rise to a virtual image 15 cm behind the mirror. Determine the magnification of the image and the type of mirror used.
- Determine whether the given object represents a scalar data-type in Python
- How to identify the type of an Image instance using FabricJS?
- Converting an image to ASCII image in Python
- Flipping an Image in Python
- An object is 2 m from a lens which forms an erect image one-fourth (exactly) the size of the object. Determine the focal length of the lens. What type of lens is this?
- Find the minimal data type of an array-like in Python
- Cartooning an Image using OpenCV in Python?
- How to compute the aspect ratio of an object in an image using OpenCV Python?
- What are the three factors that determine the type of house to build?
- Python Program to detect the edges of an image using OpenCV