Detecting if a file is an image in Python
Solution 1
Assuming:
>>> files = {"a_movie.mkv", "an_image.png", "a_movie_without_extension", "an_image_without_extension"}
And they are proper movie and image files in script folder.
You can use builtin mimetypes module, but it won't work without extensions.
>>> import mimetypes
>>> {file: mimetypes.guess_type(file) for file in files}
{'a_movie_without_extension': (None, None), 'an_image.png': ('image/png', None), 'an_image_without_extension': (None, None), 'a_movie.mkv': (None, None)}
Or call the unix command file
. This works without extensions, but not in Windows:
>>> import subprocess
>>> def find_mime_with_file(path):
... command = "/usr/bin/file -i {0}".format(path)
... return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).communicate()[0].split()[1]
...
>>> {file: find_mime_with_file(file) for file in files}
{'a_movie_without_extension': 'application/octet-stream;', 'an_image.png': 'image/png;', 'an_image_without_extension': 'image/png;', 'a_movie.mkv': 'application/octet-stream;'}
Or you try to open it with PIL, and check for errors, but needs PIL installed:
>>> from PIL import Image
>>> def check_image_with_pil(path):
... try:
... Image.open(path)
... except IOError:
... return False
... return True
...
>>> {file: check_image_with_pil(file) for file in files}
{'a_movie_without_extension': False, 'an_image.png': True, 'an_image_without_extension': True, 'a_movie.mkv': False}
Or, for simplicity, as you say, just check extensions, it's the best way I think.
>>> extensions = {".jpg", ".png", ".gif"} #etc
>>> {file: any(file.endswith(ext) for ext in extensions) for file in files}
{'a_movie_without_extension': False, 'an_image.png': True, 'an_image_without_extension': False, 'a_movie.mkv': False}
Solution 2
You should use a library for this. Note that extension != file type, because you can change the extension to a .jpg file, open it with paint and paint will interpret it like a jpeg (for example). You should check How to find the mime type of a file in python?.
Related videos on Youtube

Comments
-
Otto Allmendinger 10 months
Is there any general way to detect if a file is a image (jpg, bmp, png, etc...)
Or is making a list of the file extensions and doing a one-by-one comparison the only way?
-
Admin almost 12 yearsAccording to the standard python file types docs.python.org/c-api/concrete.html image file isn't standard, so I suppose some external module will be required.
-
Aaron Digulla over 9 yearsUse the
imghdr
module. See How to check if a file is a valid image file?
-
-
Andreas Jung almost 12 yearsThis was already mentioned - this should be a comment, not an answer
-
matchew over 10 years+1 noting for others that the use of
file
or option two worked best for my use case where I was crawling retrieving images that were returned w/o an extension and need to save them as either .jpg/.png -
Smit Mehta about 5 yearsThere is also an easy way to this...."if 'file' in request.files:" try this out if there is file then it will return true..
-
Devendra Bhat over 4 yearsI was searching for the last solution you gave. Thanks a lot.