问题
I managed to store a picture in the Google App engine blob (I can see it in the Blob Viewer from the dashboard, and also in my app using a serving handler).. However, now that i have this picture there..i want to resize it while serving it to the client...Problem is that i can't do that...I can't make an Image out of that blob...This is my code :
from google.appengine.api import images
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
....
class Image(webapp2.RequestHandler):
def get(self,id):
product = Product.by_id(int(id))
logging.info('pic key is' + str(product.small_pic.key()))
img = images.Image(blob_key=str(product.small_pic.key()))
img.im_feeling_lucky() # do a transform, otherwise GAE complains.
img.execute_transforms(output_encoding=images.JPEG,quality=1)
if img:
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(img)
else:
self.error(404)
The code from above is taken from this thread : GAE: How to get the blob-image height
When i run the code from above ex /img/373 i get the error :
The image "http:..../img/373" cannot be displayed because it contains errors How can i do this ?..What i want is to find out way to transform that blob in an image and then process the image...
回答1:
You don't need to pipe that image through your application. The gae has a service for resizing images:
from google.appengine.api.images import get_serving_url
url = get_serving_url( "blobkey")
Then append one of https://developers.google.com/appengine/docs/python/images/functions#imgsize values to that url and you're done.
回答2:
The first answer was close. This slight refinement lets you adjust the size explicitly:
from google.appengine.api.images import get_serving_url
url = get_serving_url( "blobkey",size=1024)
In the above code, size=1024 dynamically resizes the source blob image to be 1024 pixels on the longest side while maintaining the original proportion of the image.
来源:https://stackoverflow.com/questions/15997213/processing-image-from-the-blob-gae