Python Google App Engine Image object

后端 未结 2 526
误落风尘
误落风尘 2020-12-06 15:21

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

This:

img = images.Image(blob_key=image)
logging.info(img.size)
self.response.head         


        
相关标签:
2条回答
  • 2020-12-06 15:56

    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()
    
    0 讨论(0)
  • 2020-12-06 16:08

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

    logging.info((img.width, img.height))
    
    0 讨论(0)
提交回复
热议问题