简体   繁体   English

Python App Engine上传了图像内容类型

[英]Python App Engine uploaded image content-type

I know that I can accept image uploads by having a form that POSTs to App Engine like so: 我知道我可以接受图片上传,方法是将表格发送到App Engine,如下所示:

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

Then in the Python code I can do something like 然后在Python代码中我可以做类似的事情

image = self.request.get("img")

But how can I figure out what the content-type of this image should be when later showing it to the user? 但是,如何在以后向用户显示该图像时,如何确定该图像的内容类型? It seems the most robust way would be to figure this out from the image data itself, but how to get that easily? 似乎最强大的方法是从图像数据本身中解决这个问题,但是如何轻松地解决这个问题呢? I did not see anything suitable in the google.appengine.api images package. 我没有在google.appengine.api图片包中看到任何合适的内容。

Should I just look for the magic image headers in my own code, or is there already a method for that somewhere? 我应该只在我自己的代码中查找魔术图像标题,还是已经有某种方法可以在某处?

Edit: 编辑:

Here's the simplistic solution I ended up using, seems to work well enough for my purposes and avoids having to store the image type as a separate field in the data store: 这是我最终使用的简单解决方案,似乎适用于我的目的,并避免必须将图像类型存储为数据存储中的单独字段:

# 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

Instead of using self.request.get(fieldname), use self.request.POST[fieldname]. 而不是使用self.request.get(fieldname),请使用self.request.POST [fieldname]。 This returns a cgi.FieldStorage object (see the Python library docs for details), which has 'filename', 'type' and 'value' attributes. 这将返回一个cgi.FieldStorage对象(有关详细信息,请参阅Python库文档),该对象具有“filename”,“type”和“value”属性。

Try the python mimetypes module, it will guess the content type and encoding for you, 试试python mimetypes模块,它会猜测你的内容类型和编码,

eg 例如

>>import mimetypes >>进口mimetypes

>>mimetypes.guess_type("/home/sean/desktop/comedy/30seconds.mp4") >> mimetypes.guess_type( “/家/肖恩/桌面/喜剧/ 30seconds.mp4”)

('video/mp4', None) ('video / mp4',无)

Based on my research, browsers, except for Internet Explorer (version 6 at least), determine the file mime type by using the file's extension. 根据我的研究,除了Internet Explorer(至少版本6)之外,浏览器通过使用文件的扩展名来确定文件mime类型。 Given that you want image mime types, you could use a simple Python dictionary in order to achieve this. 鉴于您需要图像mime类型,您可以使用简单的Python字典来实现此目的。

Unfortunately I don't know of any method in Python that tries to guess the image type by reading some magic bytes (the way fileinfo does in PHP). 不幸的是,我不知道Python中的任何方法都试图通过读取一些魔术字节来猜测图像类型( fileinfo在PHP中的方式)。 Maybe you could apply the EAFP (Easier to Ask Forgiveness than Permission) principle with the Google appengine image API. 也许您可以使用Google appengine图像API应用EAFP(更容易请求宽恕而不是权限)原则。

Yes, it appears that the image API does not tell you the type of image you've loaded. 是的,似乎图像API没有告诉您已加载的图像类型。 What I'd do in this case is to build that Python dictionary to map file extensions to image mime types and than try to load the image while expecting for a NotImageError() exception. 在这种情况下我要做的是构建Python字典以将文件扩展名映射到图像mime类型,然后尝试加载图像,同时期望NotImageError()异常。 If everything goes well, then I assume the mime type was OK. 如果一切顺利,那么我认为mime类型没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM