gpt4 book ai didi

python - 在 UploadHandler 中执行图像转换并将其保存到 Blobstore

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:10 24 4
gpt4 key购买 nike

我有一个用 Python 编写的关于 GAE 的页面,它 (1) 将 jpg 上传到 blobstore。那部分有效。我现在需要进行 (2) 手气不错的图像转换,然后 (3) 将其作为另一个 blob 存储在 blobstore 中。理想情况下,我想在同一个上传处理程序中执行 (1)、(2) 和 (3)。

我已经按照此处的代码进行操作,但它只执行 (1) 和 (2)。 https://developers.google.com/appengine/docs/python/images/#Python_Transforming_images_from_the_Blobstore

我已经查看了 SO,我能找到的最接近的是: Storing Filtered images on the blobstore in GAE

它将转换保存到文件(使用 Files API),然后将文件上传到 blobstore。但是,它使用 Files api,并且根据以下内容,不推荐使用 Files API。 https://developers.google.com/appengine/docs/python/blobstore/#Python_Writing_files_to_the_Blobstore

在我的模型中,我有一个 BlobKeyProperty,它在 blobstore 中存储对图像的引用

class ImageModel(ndb.Model):
imagetoserve = ndb.BlobKeyProperty(indexed=False)

到目前为止,这是上传处理程序代码:

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api.images import get_serving_url
from google.appengine.api import images

upload_files = self.get_uploads('imgfile') # 'file' is file upload field in the form
blob_info = upload_files[0]

imgtmp = images.Image(blob_info)
imgtmp.im_feeling_lucky()
img = ImageModel()
img.imagetoserve = imgtmp
img.put()

我的问题是在这一行:

img.imagetoserve = imgtmp

该模型是一个 blobkey 属性,但我正在为其提供图像,显然会导致类型不匹配的错误。如何执行将转换后的 imgtmp 上传到 blobstore、捕获 blobkey 并将引用保存到我的模型的步骤?

最佳答案

阅读Blobstore API Documentation

不幸的是,传统上您会通过文件 API 完成此操作,但由于他们不赞成使用 GCS,您可以执行以下操作(您可以填写缺失的部分):(来自 this example)

import cloudstorage as gcs
from google.appengine.ext import blobstore

class ImageModel(ndb.Model):
image_filename = ndb.StringProperty(indexed=False)

@property
def imagetoserve(self):
return blobstore.create_gs_key(self.image_filename)

BUCKET = "bucket_to_store_image\\"

with gcs.open(BUCKET + blob_info.filename, 'w', content_type='image/png') as f:
f.write(imgtmp.execute_transforms())

img = ImageModel()
img.image_filename = BUCKET + blob_info.filename
img.put()

关于python - 在 UploadHandler 中执行图像转换并将其保存到 Blobstore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18140142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com