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

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

问题描述

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



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

有属性错误: p>

  AttributeError:'Image'对象没有属性'size'

所以Google应用程序引擎的Image实例没有大小?



那么这是如何工作的:

  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对象。
所以最终的解决方案如下所示:
$ b $ pre $ class $ Man $ $ $ $ $ $ $ $ $ image $ = blobstore.BlobReferenceProperty()

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

通过这种方式,我可以将图像url解析到我的页面中,例如:

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


解决方案

Google提供的另一种解决方案,用于提供图片和调整图片
Google可以使用Google高性能图片服务为您提供图片。这意味着:


  • 您必须创建一次,blobstore中的图片的serving_url使用:get_serving_url

  • 您可以更改提供的图像的大小。原文未更改

  • Google几乎为您免费提供图片。你不需要处理程序。您只支付带宽



以下是一个示例。您可以更改= s0,更改大小。 s $返回原始大小。

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

get_serving_url文档: 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)
尝试:#get url with size = 0
dyn.serving_url = images.get_serving_url(dyn.blob_ref,size = None,secure_url = True)
除了DeadlineExceededError:
try:#有时这个请求失败,重试。这总是正常工作
dyn.serving_url = images.get_serving_url(dyn.blob_ref,size = None,secure_url = True)
除了DeadlineExceededError:
logging.error('Image API get_serving_url deadline error after重试'%(dyn.key()。name()))
return None
dyn.put()


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

This:

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

Has Attribute error:

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

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

So then how does this work:

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)

What am I missing?

EDIT:

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)

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 %}
/>

解决方案

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:

  • you have to create once, a serving_url for images in the blobstore using : get_serving_url
  • You can change the size of the served image. The original is not changed
  • Google will serve the images almost for free for you. You do not need a handler. You only pay the bandwidth

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 docs: https://developers.google.com/appengine/docs/python/images/functions

Code :

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图像对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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