首页 > 解决方案 > 如何在 Google App Engine 中获取 26 字节的非 JPG 图像的宽度和高度?

问题描述

我想获取存储在谷歌云存储中的图像的宽度和高度,而不将其加载到内存中。

我在 stackoverflow 上找到了答案,说明如果图像类型不是JPEG,或者TIFF我们可以通过仅在内存中加载 26 个字节来获取图像的宽度和高度。但我无法想出这样做的方法。

我没有找到任何相关的代码片段或文章。

我还想知道是否可以通过使用动态转换PNG创建的图像来实现get_serving_url()。并加载该图像的 26 个字节。

标签: google-app-enginegoogle-cloud-storagegoogle-app-engine-python

解决方案


好的,我来打针。试试这个:

from google.appengine.api import images
import urllib2

image_url   = "http://....."

req         = urllib2.Request(image_url, headers={'Range': 'bytes=0-30'})
resp        = urllib2.urlopen(req)
image_bytes = resp.read()
image       = images.Image(image_bytes)

print image.width 
print image.height

resp.close()

推荐阅读