Finding MIME type for a data stream

Tobiah toby at tobiah.org
Thu Mar 8 18:40:13 EST 2012


> I have to assume you're talking python 2, since in python 3, strings 
> cannot generally contain image data.  In python 2, characters are pretty 
> much interchangeable with bytes.

Yeah, python 2


> if you're looking for a specific, small list of file formats, you could 
> make yourself a signature list.  Most (not all) formats distinguish 
> themselves in the first few bytes. 

Yeah, maybe I'll just do that.  I'm alowing users to paste
images into a rich-text editor, so I'm pretty much looking 
at .png, .gif, or .jpg.  Those should be pretty easy to 
distinguish by looking at the first few bytes.  

Pasting images may sound weird, but I'm using a jquery
widget called cleditor that takes image data from the
clipboard and replaces it with inline base64 data.  
The html from the editor ends up as an email, and the
inline images cause the emails to be tossed in the
spam folder for most people.  So I'm parsing the
emails, storing the image data, and replacing the
inline images with an img tag that points to a 
web2py app that takes arguments that tell it which 
image to pull from the database.  

Now that I think of it, I could use php to detect the
image type, and store that in the database.  Not quite
as clean, but that would work.

Tobiah



More information about the Python-list mailing list