Skip to content

Instantly share code, notes, and snippets.

@uuklanger
Created February 10, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uuklanger/4824ffb6c053e8163b8dc00621fb31e5 to your computer and use it in GitHub Desktop.
Save uuklanger/4824ffb6c053e8163b8dc00621fb31e5 to your computer and use it in GitHub Desktop.
HOWTO - Check to see if File is Image
#!/usr/bin/env python3
#
# http://www.blog.pythonlibrary.org/2020/02/09/how-to-check-if-a-file-is-a-valid-image-with-python/
#
import imghdr # for imghdr
from PIL import Image # for Image.open
path = 'python.jpg'
imghdr.what(path)
# returns 'jpeg'
path = 'python.png'
imghdr.what(path)
# returns 'png'
img = Image.open('all_python.jpg')
img.format
# returns 'JPEG'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment