>>> mimetypes.guess_type('picture.jpg')
('image/jpeg', None)
Now I have a file-like object, (eg. stingIO), which content is image's data
How can i detect the mimetypes from a file-like object
The mimetypes module converts between a filename or URL and the MIME type associated with the filename extension. Conversions are provided from filename to MIME type and from MIME type to filename extension; encodings are not supported for the latter conversion.
Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email to support: – Text in character sets other than ASCII. – Non-text attachments: audio, video, images, application programs etc. – Message bodies with multiple parts.
A MIME type consists of two parts: a type and a subtype. Currently, there are ten registered types: application, audio, example, font, image, message, model, multipart, text, and video.
The python mimetype standard module maps filenames to mime-types and vice versa. To use it, you'll need a filename or a mime-type, in which case it'll give you back a possible file extension.
It won't/doesn't determine the mime-type based on a file's contents. You need another type of tool to do that. Libmagic, the library behind the unix file command, is one of those tools. The filemagic module (https://pypi.python.org/pypi/filemagic/1.6) is a python interface to libmagic.
import urllib2
import magic
img_data = urllib2.urlopen('https://www.google.com/images/srpr/logo11w.png').read()
# You can add flags
# magic.Magic(flags=magic.MAGIC_MIME_TYPE) for take "/image/png"
m = magic.Magic()
print m.id_buffer(img_data)
m.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With