Created
July 31, 2016 18:24
-
-
Save NanoDano/f6db0d47820015af87b82dbdd594474e to your computer and use it in GitHub Desktop.
See if a file is a JPG with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#is_jpeg.py - Does the file have a JPEG binary signature? | |
import sys | |
import binascii | |
jpeg_signatures = [ | |
binascii.unhexlify(b'FFD8FFD8'), | |
binascii.unhexlify(b'FFD8FFE0'), | |
binascii.unhexlify(b'FFD8FFE1') | |
] | |
with open(sys.argv[1], 'rb') as file: | |
first_four_bytes = file.read(4) | |
if first_four_bytes in jpeg_signatures: | |
print("JPEG detected.") | |
else: | |
print("File does not look like a JPEG.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment