gpt4 book ai didi

python-3.x - 使用 Python37 运行时使用 Cloud Functions 生成缩略图

转载 作者:行者123 更新时间:2023-12-04 01:17:23 26 4
gpt4 key购买 nike

我有一个由 Firebase 存储触发的 Google Cloud 功能,我想生成缩略图。

虽然 Node.js 文档有 example that uses ImageMagick python 运行时没有这样的等价物。

牢记性能的可接受方法是什么?会Pillow-SIMD在云功能中工作?

或者我应该去 App Engine 生成缩略图并使用 Images service ?

最佳答案

您可以使用 wand ,与 ImageMagick 的绑定(bind),以及 google-cloud-storage 将图像上传到存储桶后自动调整大小。

requirements.txt :

google-cloud-storage
wand

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 进行部署工具:
$ gcloud beta functions deploy make_thumbnail \
--runtime python37 \
--trigger-bucket gs://[your-bucket-name].appspot.com

关于python-3.x - 使用 Python37 运行时使用 Cloud Functions 生成缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51885962/

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