使用Python37运行时使用Cloud Functions生成缩略图 [英] Generating thumbnails with Cloud Functions using the Python37 runtime

本文介绍了使用Python37运行时使用Cloud Functions生成缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Firebase Storage触发的Google Cloud Function,我想生成缩略图.

I have a Google Cloud Function triggered by Firebase Storage, and I want to generate thumbnails.

Node.js文档中有一个使用ImageMagick的示例在python运行时中没有这样的等效项.

While the Node.js docs have an example that uses ImageMagick there is no such equivalent for the python runtime.

什么是可以考虑性能的可接受方法?枕头SIMD 是否可以在云功能中工作?

What would be an acceptable approach keeping performance in mind ? Would Pillow-SIMD work in a cloud function ?

或者我应该使用App Engine生成缩略图并使用图片服务?

Or should I go App Engine for thumbnail generation and use the Images service ?

推荐答案

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

You can use wand, a binding to ImageMagick, along with google-cloud-storage to resize an image automatically once it's uploaded to a storage bucket.

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 工具进行部署:

Then you can deploy it with the gcloud tool:

$ gcloud beta functions deploy make_thumbnail \
    --runtime python37 \
    --trigger-bucket gs://[your-bucket-name].appspot.com

这篇关于使用Python37运行时使用Cloud Functions生成缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆