Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created July 31, 2016 18:24
Show Gist options
  • Save NanoDano/f6db0d47820015af87b82dbdd594474e to your computer and use it in GitHub Desktop.
Save NanoDano/f6db0d47820015af87b82dbdd594474e to your computer and use it in GitHub Desktop.
See if a file is a JPG with Python
#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