Like python mimetypes.guess_type from a file object

>>> mimetypes.guess_type('picture.jpg')
('image/jpeg', None)

Now I have a file-like object (like stingIO) whose contents are image data.

How can I detect mimetypes from a file object

+4
source share
1 answer

The standard python mimetype module maps file names to mime types and vice versa. To use it, you will need a file name or a mime type, in which case it will return you a possible file extension.

/ mime . . Libmagic, unix, . (https://pypi.python.org/pypi/filemagic/1.6) python libmagic.

import urllib2
import magic

img_data = urllib2.urlopen('https://www.google.com/images/srpr/logo11w.png').read()
m = magic.Magic()
print m.id_buffer(img_data)
m.close()
+5

All Articles