Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21406

Re: Finding MIME type for a data stream

Date 2012-03-08 17:56 -0500
From Dave Angel <d@davea.name>
Subject Re: Finding MIME type for a data stream
References <%U96r.32867$082.25394@newsfe04.iad> <mailman.518.1331244722.3037.python-list@python.org> <3oa6r.31449$L12.8825@newsfe23.iad>
Newsgroups comp.lang.python
Message-ID <mailman.521.1331247434.3037.python-list@python.org> (permalink)

Show all headers | View raw


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(d8) (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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-08 13:55 -0800
  Re: Finding MIME type for a data stream Dave Angel <d@davea.name> - 2012-03-08 17:11 -0500
    Re: Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-08 14:28 -0800
      Re: Finding MIME type for a data stream Dave Angel <d@davea.name> - 2012-03-08 17:56 -0500
        Re: Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-08 15:40 -0800
          Re: Finding MIME type for a data stream Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-03-08 21:04 -0500
            Re: Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-09 08:40 -0800
          Re: Finding MIME type for a data stream Jon Clements <joncle@googlemail.com> - 2012-03-08 18:31 -0800
            Re: Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-09 09:53 -0800
    Re: Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-08 14:34 -0800
      Re: Finding MIME type for a data stream Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2012-03-09 03:12 +0100
        Re: Finding MIME type for a data stream Tobiah <toby@tobiah.org> - 2012-03-09 08:35 -0800
  Re: Finding MIME type for a data stream Peter Otten <__peter__@web.de> - 2012-03-09 11:50 +0100

csiph-web