Skip to content

Instantly share code, notes, and snippets.

@seoh
Last active July 18, 2021 14:56
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 seoh/4c867b92f5ffbc78e0584b2c5b4c62b5 to your computer and use it in GitHub Desktop.
Save seoh/4c867b92f5ffbc78e0584b2c5b4c62b5 to your computer and use it in GitHub Desktop.
detect image mime type
name = 'image.jpg'
with open(name, 'rb') as f:
content = f.read()
head = content[0:3].hex()
mime = 'jpg'
if head == '89504e': mime = 'png'
elif head == '474946': mime = 'gif'
# else head == 'ffd8ff': mime = 'jpg'
print(mime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment