Skip to content

Instantly share code, notes, and snippets.

@yangg
Created February 15, 2012 06:39
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 yangg/1833796 to your computer and use it in GitHub Desktop.
Save yangg/1833796 to your computer and use it in GitHub Desktop.
Get image type
#!/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