August 2018
Intermediate to advanced
366 pages
10h 14m
English
The imghdr module can help us understand what kind of image file we are facing:
import imghdr
def detect_image_format(filename):
return imghdr.what(filename)
This allows us to detect the format of any image on disk or of a stream of bytes provided:
>>> print(detect_image_format('~/Pictures/avatar.jpg'))
'jpeg'
>>> with open('~/Pictures/avatar.png', 'rb') as f:
... print(detect_image_format(f))
'png'