我有一个由Firebase Storage触发的Google Cloud函数,我想生成缩略图。
虽然Node.js文档有一个example that uses ImageMagick,但是对于python运行时却没有这样的等价物。
考虑到性能,什么是可接受的方法?Pillow-SIMD能在云函数中工作吗?
或者我应该使用App Engine生成缩略图并使用Images service?
发布于 2018-08-24 05:20:53
您可以使用绑定到ImageMagick的wand
以及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
发布于 2018-08-17 16:57:09
在使用Python运行时时,我错误地认为ImageMagick没有安装在Google Cloud Function环境中,因为它没有文档记录。
但实际上是这样的,云函数如下:
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
https://stackoverflow.com/questions/51885962
复制相似问题