Path: csiph.com!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'detect': 0.21; 'from:addr:web.de': 0.23; 'skip:m 30': 0.24; 'subject:data': 0.25; 'looks': 0.27; "i'm": 0.28; 'correct': 0.29; 'far,': 0.30; 'header,': 0.30; "i've": 0.32; "can't": 0.33; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'file': 0.34; 'header:X -Complaints-To:1': 0.34; 'to:addr:python-list': 0.35; 'data.': 0.36; 'received:org': 0.36; 'but': 0.37; 'some': 0.38; 'data': 0.38; 'skip:o 20': 0.38; 'might': 0.40; 'to:addr:python.org': 0.40; 'serving': 0.68; 'subject:stream': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Finding MIME type for a data stream Date: Fri, 09 Mar 2012 11:50:04 +0100 Organization: None References: <%U96r.32867$082.25394@newsfe04.iad> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849862.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331290226 news.xs4all.nl 6937 [2001:888:2000:d::a6]:56353 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21422 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'