Created
February 15, 2012 06:39
-
-
Save yangg/1833796 to your computer and use it in GitHub Desktop.
Get image type
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def get_image_type(filename): | |
with open(filename, 'rb') as f: | |
data = f.read(10).encode('hex') | |
ftype = None | |
if data.startswith('ffd8'): | |
ftype = 'jpeg' | |
elif data.startswith('474946'): | |
ftype = 'gif' | |
elif data.startswith('89504e470d0a1a0a'): | |
ftype = 'png' | |
elif data.startswith('424d'): | |
ftype = 'bmp' | |
return ftype | |
import sys | |
if __name__ == '__main__': | |
for f in sys.argv[1:]: | |
print '%s: %s' % (f, get_image_type(f)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment