我有一个由 Firebase 存储触发的 Google Cloud 函数,我想生成缩略图。
虽然 Node.js 文档有一个使用 ImageMagick 的示例 https://cloud.google.com/functions/docs/tutorials/imagemagickpython 运行时没有这样的等效项。
考虑到性能的可接受方法是什么?会枕式SIMD https://github.com/uploadcare/pillow-simd在云函数中工作?
或者我应该使用 App Engine 生成缩略图并使用图片服务 https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.images ?
您可以使用wand https://pypi.org/project/Wand/,与 ImageMagick 的绑定,以及google-cloud-storage https://pypi.org/project/google-cloud-storage/上传到存储桶后自动调整图像大小。
In requirements.txt
:
google-cloud-storage
wand
In main.py
:
from wand.image import Image
from google.cloud import storage
client = storage.Client()
PREFIX = "thumbnail"
def make_thumbnail(data, context):
# Don't generate a thumbnail for a thumbnail
if data['name'].startswith(PREFIX):
return
# Get the bucket which the image has been uploaded to
bucket = client.get_bucket(data['bucket'])
# Download the image and resize it
thumbnail = Image(blob=bucket.get_blob(data['name']).download_as_string())
thumbnail.resize(100, 100)
# Upload the thumbnail with the filename prefix
thumbnail_blob = bucket.blob(f"{PREFIX}-{data['name']}")
thumbnail_blob.upload_from_string(thumbnail.make_blob())
然后您可以使用以下命令部署它gcloud
tool:
$ gcloud beta functions deploy make_thumbnail \
--runtime python37 \
--trigger-bucket gs://[your-bucket-name].appspot.com
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)