Last active
July 18, 2021 14:56
-
-
Save seoh/4c867b92f5ffbc78e0584b2c5b4c62b5 to your computer and use it in GitHub Desktop.
detect image mime type
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
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