Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'bytes.': 0.07; 'python': 0.08; '(there': 0.09; 'utility,': 0.09; 'thanks!': 0.14; '(ff)': 0.16; 'exe': 0.16; 'formats,': 0.16; 'received:192.168.1.104': 0.16; 'cc:addr:python-list': 0.16; 'question.': 0.16; 'wrote:': 0.18; 'help.': 0.19; 'cc:no real name:2**0': 0.21; 'maybe': 0.21; 'appropriate': 0.22; 'header:In-Reply-To:1': 0.22; 'assume': 0.22; 'sfxlen:0': 0.23; 'starts': 0.24; 'string': 0.24; 'subject:data': 0.25; 'windows': 0.26; 'cc:2**0': 0.26; 'there.': 0.27; 'module.': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'generally': 0.30; 'modules,': 0.30; 'pretty': 0.31; 'quite': 0.31; 'list': 0.32; 'header:User-Agent:1': 0.33; 'file': 0.34; 'surprised': 0.34; '(not': 0.35; 'something': 0.35; 'list.': 0.35; 'things': 0.35; 'external': 0.35; 'data.': 0.36; 'example,': 0.37; 'run': 0.37; 'received:192': 0.38; 'could': 0.38; 'should': 0.38; 'data': 0.38; 'characters': 0.39; 'received:192.168.1': 0.39; 'cannot': 0.39; 'called': 0.40; 'back': 0.60; 'your': 0.61; 'talking': 0.62; 'zip': 0.62; 'believe': 0.65; 'yourself': 0.69; 'header:Reply- To:1': 0.70; 'reply-to:no real name:2**0': 0.72; 'distinguish': 0.84; 'subject:stream': 0.84 Date: Thu, 08 Mar 2012 17:56:36 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.27) Gecko/20120216 Thunderbird/3.1.19 MIME-Version: 1.0 To: Tobiah Subject: Re: Finding MIME type for a data stream References: <%U96r.32867$082.25394@newsfe04.iad> <3oa6r.31449$L12.8825@newsfe23.iad> In-Reply-To: <3oa6r.31449$L12.8825@newsfe23.iad> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:xxci6ObCx+PaLAaDVMEzuglnA9n31Z/mNpfMzLeS9MD pPyi807Kre6uDWOrE79ofTfwFJK+UI3vBSouOveFdF/cvX6scW +HO1ThLdFEZ3lxmQOSUc74KVw0WfxedXKzkKR+0j/NX1wX4GS1 E6VS9wxtRRegEjyq+atbJWbXN/5ADZNP3sPxLazI207Do6y2EJ 42neY5c+0fejhvaOuJEKTnqQwdik1AL0spbLFY3EX7Xub/hbP9 LSSXeSjX+tu5aMD7gLomoz84dApOauA19cXuu1ZHeJZZGSSrNO 5eU9YJk9Vt6HvOe/qPvtZoc5PMGJSb+dTIt7YVGIFfObInFtw= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331247434 news.xs4all.nl 6848 [2001:888:2000:d::a6]:35524 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21406 On 03/08/2012 05:28 PM, Tobiah wrote: > > > > 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