Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finding MIME type for a data stream

Reply
Thread Tools

Finding MIME type for a data stream

 
 
Tobiah
Guest
Posts: n/a
 
      03-08-2012
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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      03-08-2012
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?

--

DaveA

 
Reply With Quote
 
 
 
 
Tobiah
Guest
Posts: n/a
 
      03-08-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
 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      03-08-2012
Also, I realize that I could write the data to a file
and then use one of the modules that want a file path.
I would prefer not to do that.

Thanks

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      03-08-2012
On 03/08/2012 05:28 PM, Tobiah wrote:
> <snip>
>
>
> 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


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.

Anyway, I don't know any way in the standard lib to distinguish
arbitrary image formats. (There very well could be one.) The file
program I referred to was an external utility, which you could run with
the multiprocessing module.

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. For example, a standard zip file
starts with "PK" for Phil Katz. A Windows exe starts with "MZ" for
Mark Zbikowsky. And I believe a jpeg file starts hex(d (ff) (e0) (ff)

If you'd like to see a list of available modules, help() is your
friend. You can start with help("modules") to see quite a long list.
And I was surprised how many image related things already are there. So
maybe there's something I don't know about that could help.

--

DaveA

 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      03-08-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
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-09-2012
On Thu, 08 Mar 2012 15:40:13 -0800, Tobiah <(E-Mail Removed)> declaimed
the following in gmane.comp.python.general:


> 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.


In Windows, I'd expect "device independent bitmap" to be the result
of a clipboard image...
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Irmen de Jong
Guest
Posts: n/a
 
      03-09-2012
On 8-3-2012 23:34, Tobiah wrote:
> Also, I realize that I could write the data to a file
> and then use one of the modules that want a file path.
> I would prefer not to do that.
>
> Thanks
>


Use StringIO then, instead of a file on disk

Irmen

 
Reply With Quote
 
Jon Clements
Guest
Posts: n/a
 
      03-09-2012
On Thursday, 8 March 2012 23:40:13 UTC, Tobiah wrote:
> > 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


Something like the following might be worth a go:
(untested)

from PIL import Image
img = Image.open(StringIO(blob))
print img.format

HTH
Jon.

PIL: http://www.pythonware.com/library/pi...book/image.htm
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      03-09-2012
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.


After some try-and-error and a look into example.py:

>>> m = magic.open(magic.MAGIC_MIME_TYPE)
>>> m.load()

0
>>> sample = open("tmp.png").read()
>>> m.buffer(sample)

'image/png'


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
tomcat 4.x : setting mime type for a directory or setting a default mime type CJ Java 1 10-29-2004 07:51 PM
how 2 convert mime to txt or extract words from mime joe Perl Misc 0 04-07-2004 12:34 AM
JSP upload - detecting file type by mime type? what is application/octet-stream? Stephen Riek Java 0 09-19-2003 03:52 AM
Receiving zip files via Mime::Parser/Mime::Decoder Jan Arickx Perl Misc 0 08-25-2003 08:24 PM



Advertisments