我知道,我可以有一個表單,POST到App Engine的,像這樣接受圖片上傳:Python應用程序引擎上傳的圖片內容類型
<form action="/upload_received" enctype="multipart/form-data" method="post">
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Upload Image"></div>
</form>
然後在Python代碼我可以這樣做
image = self.request.get("img")
但是,我怎麼才能找出這個圖像的內容類型應該是什麼時候以後再向用戶展示呢?看起來最可靠的方法是從圖像數據本身中弄清楚這一點,但如何輕鬆實現?我在google.appengine.api圖片包中沒有看到任何適合的內容。
我應該只是在我自己的代碼中尋找魔法圖像標題,或者是否已經存在某種方法?
編輯:
下面是簡單的解決方案,我結束了使用,似乎工作不夠好,我的目的,避免了對圖像類型存儲作爲數據存儲在不同的領域:
# Given an image, returns the mime type or None if could not detect.
def detect_mime_from_image_data(self, image):
if image[1:4] == 'PNG': return 'image/png'
if image[0:3] == 'GIF': return 'image/gif'
if image[6:10] == 'JFIF': return 'image/jpeg'
return None
請帖子鏈接 – crizCraig 2011-05-27 18:30:44
這給了我錯誤「AttributeError:UnicodeMultiDict實例沒有__call__方法」 – zakdances 2011-10-14 02:35:34
@yourfriendzak你仍然使用parens'()',而不是方括號'[]'。 – 2011-10-16 07:28:05