Python Google App Engine Image object

前提是你 提交于 2019-11-26 21:45:01

问题


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

回答1:


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



回答2:


It looks like the GAE version of PIL doesn't implement .size. Use something like this instead:

logging.info((img.width, img.height))


来源:https://stackoverflow.com/questions/13810823/python-google-app-engine-image-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!