Finding MIME type for a data stream

Tobiah toby at tobiah.org
Thu Mar 8 17:28:15 EST 2012


On 03/08/2012 02:11 PM, Dave Angel wrote:
> On 03/08/2012 04:55 PM, Tobiah wrote:
>> I'm pulling image data from a database blob, and serving
>> it from a web2py app.  I have to send the correct
>> Content-Type header, so I need to detect the image type.
>>
>> Everything that I've found on the web so far, needs a file
>> name on the disk, but I only have the data.
>>
>> It looks like the 'magic' package might be of use, but
>> I can't find any documentation for it.
>>
>> Also, it seems like image/png works for other types
>> of image data, while image/foo does not, yet I'm afraid
>> that not every browser will play along as nicely.
>>
>> Thanks!
>>
>> Tobiah
> 
> First step, ask the authors of the database what format of data this 
> blob is in.
> 
> Failing that, write the same data locally as a binary file, and see what 
> application can open it.  Or if you're on a Linux system, run file on 
> it.  "file" can identify most data formats (not just images) just by 
> looking at the data.
> 
> That assumes, of course, that there's any consistency in the data coming 
> out of the database.  What happens if next time this blob is an Excel 
> spreadsheet?
> 


I should simplify my question.  Let's say I have a string
that contains image data called 'mystring'.

I want to do

mime_type = some_magic(mystring)

and get back 'image/jpg' or 'image/png' or whatever is
appropriate for the image data.

Thanks!

Tobiah



More information about the Python-list mailing list