当我尝试从 URL 获取图像并将其响应中的字符串转换为 App Engine 中的 Image
时,我收到了上述消息的错误。
from google.appengine.api import urlfetch
def fetch_img(url):
try:
result = urlfetch.fetch(url=url)
if result.status_code == 200:
return result.content
except Exception, e:
logging.error(e)
url = "http://maps.googleapis.com/maps/api/staticmap?center=Narita+International+Airport,Narita,Chiba+Prefecture,+Japan&zoom=18&size=512x512&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false"
img = fetch_img(url)
# As the URL above tells, its size is 512x512
img = Image.fromstring('RGBA', (512, 512), img)
根据 PIL , size 选项假设是一个像素元组。这是我指定的。谁能指出我的误解?
原文由 suzukimilanpaak 发布,翻译遵循 CC BY-SA 4.0 许可协议
image 返回的数据是图像本身而不是 RAW RGB 数据,因此您不需要将其作为原始数据加载,而是只需将该数据保存到文件中,它将成为有效图像或使用 PIL 打开它,例如(我已将您的代码转换为不使用 appengine api,以便任何安装了普通 python 的人都可以运行该示例)
输出: