【问题标题】:Processing image from the blob GAE处理来自 blob GAE 的图像
【发布时间】:2013-04-06 12:09:54
【问题描述】:

我设法在 Google App 引擎 blob 中存储了一张图片(我可以在仪表板的 Blob 查看器中看到它,也可以在我的应用程序中使用服务处理程序看到它).. 但是,现在我有这张图片了..我想在为客户提供服务时调整它的大小...问题是我不能这样做...我不能用那个 blob 制作图像.. .这是我的代码:

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)

上面的代码取自这个线程:GAE: How to get the blob-image height

当我从上面的 ex /img/373 运行代码时,我得到了错误:

图片“http:..../img/373”无法显示,因为它包含错误 我该怎么做?..我想要找到在图像中转换该 blob 的方法,然后处理图像...

【问题讨论】:

  • execute_transforms 中,您将编码指定为JPEG,并在响应标头中将其指定为PNG。这应该是?
  • 在我的许多尝试中,我评论了img.execute_transforms(output_encoding=images.JPEG,quality=1)这一行,但仍然没有任何改变:(

标签: python image google-app-engine blob


【解决方案1】:

第一个答案很接近。这种细微的改进让您可以明确地调整大小:

from google.appengine.api.images import get_serving_url
url = get_serving_url( "blobkey",size=1024)

在上述代码中,size=1024 动态调整源 blob 图像的大小,使其最长边为 1024 像素,同时保持图像的原始比例。

【讨论】:

    【解决方案2】:

    您无需通过应用程序传送该图像。 gae 有调整图片大小的服务:

    from google.appengine.api.images import get_serving_url
    url = get_serving_url( "blobkey")
    

    然后附加https://developers.google.com/appengine/docs/python/images/functions#imgsize 值之一 到那个网址,你就完成了。

    【讨论】:

    • 我是这样做的......它更有效,因为我可以保留一个大图像,然后将它的大小设置为我想要的任意大小:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2011-06-15
    • 2011-07-28
    相关资源
    最近更新 更多