Python Google App Engine Image 对象 [英] Python Google App Engine Image object

查看:47
本文介绍了Python Google App Engine Image 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 图像库 PIL 和 Google App Engine Blobstore...

Using Python Image Library PIL and Google App Engine Blobstore...

这个:

img = images.Image(blob_key=image)
logging.info(img.size)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(img)

有属性错误:

AttributeError: 'Image' object has no attribute 'size'

所以来自谷歌应用引擎的图像实例没有大小?

So the Image instance from google app engine does not have size?

那么这是如何工作的:

img = images.Image(blob_key=image)
img.resize(width, height)
img.im_feeling_lucky()
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)

我错过了什么?

修复是使用 get_serving_url 而不是使用我的图像服务器,正如@voscausa 所提议的那样.由于我的对象是由 jinja2 模板解析的,因此无法通过 jinja2 创建 Image 对象.所以最终的解决方案如下:

The fix was using the get_serving_url and not use my image server as proposed by @voscausa. Due to the fact that my object was parsed by jinja2 templating it was impossible to create a Image object via jinja2. So the final solution worked as below:

class Mandelbrot(db.Model):
  image = blobstore.BlobReferenceProperty()

@property
def image_url(self):
  return images.get_serving_url(self.image)

这样我就可以将图像 url 解析到我的页面,例如:

This way I could parse the image url to my page like:

<img src=
{% if mandelbrot.image %}
  "{{ mandelbrot.image_url }}" 
{% else %} 
  "./assets/img/preloader.gif"
{% endif %}
/>

推荐答案

我不熟悉 PIL,因为我使用 Google 的另一个解决方案来提供图像和调整图像大小.Google 可以使用 Google 高性能图像服务为您提供图像.这意味着:

I'am not familiair with PIL, because I use another solution from Google for serving and sizing images. Google can serve the images for you, using Google High Performance Image serving. This means:

  • 您必须使用 get_serving_url 为 blobstore 中的图像创建一次 services_url
  • 您可以更改所提供图片的大小.原版没变
  • Google 将几乎免费为您提供图片.您不需要处理程序.您只需支付带宽

这是一个例子.您可以更改 =s0,以更改大小.s0 返回原始大小.

Here is an example. You can change the =s0, to change the size. s0 returns the original size.

https://lh6.ggpht.com/1HjICy6ju1e2GIg83L0qdliUBmPHUgKV8FP3QGK8Qf2pHVBfwkpO_V38ifAPm-9m20q_3ueZzdRCYQNyDE3pmA695iaLunjE=s0

get_serving_url 文档:https://developers.google.com/appengine/docs/python/图像/函数

get_serving_url docs: https://developers.google.com/appengine/docs/python/images/functions

代码:

class Dynamic(db.Model):          # key : name
    name = db.StringProperty() 
    blob_ref = blobstore.BlobReferenceProperty()
    serving_url = db.LinkProperty()

dyn= Dynamic.get_by_key_name(key_name)
try :       # get url with size = 0
    dyn.serving_url = images.get_serving_url(dyn.blob_ref, size=None, secure_url=True)
except DeadlineExceededError : 
    try :             # sometimes this request fails, retry. This always works fine
        dyn.serving_url = images.get_serving_url(dyn.blob_ref, size=None, secure_url=True)
    except DeadlineExceededError :
        logging.error('Image API get_serving_url deadline error after retry' %(dyn.key().name()))                        
        return None
    dyn.put()

这篇关于Python Google App Engine Image 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆