首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python37运行时使用云函数生成缩略图

使用Python37运行时使用云函数生成缩略图
EN

Stack Overflow用户
提问于 2018-08-17 06:09:00
回答 2查看 1.7K关注 0票数 3

我有一个由Firebase Storage触发的Google Cloud函数,我想生成缩略图。

虽然Node.js文档有一个example that uses ImageMagick,但是对于python运行时却没有这样的等价物。

考虑到性能,什么是可接受的方法?Pillow-SIMD能在云函数中工作吗?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-24 05:20:53

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

requirements.txt

代码语言:javascript
运行
复制
google-cloud-storage
wand

main.py

代码语言:javascript
运行
复制
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工具部署它:

代码语言:javascript
运行
复制
$ gcloud beta functions deploy make_thumbnail \
    --runtime python37 \
    --trigger-bucket gs://[your-bucket-name].appspot.com
票数 4
EN

Stack Overflow用户

发布于 2018-08-17 16:57:09

在使用Python运行时时,我错误地认为ImageMagick没有安装在Google Cloud Function环境中,因为它没有文档记录。

但实际上是这样的,云函数如下:

代码语言:javascript
运行
复制
import wand.version


def cloud_function(request):
    print(wand.version.MAGICK_VERSION)

输出ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51885962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档