Flo*_*rth 4 python-3.x firebase google-cloud-platform google-cloud-functions firebase-storage
我有一个由 Firebase 存储触发的 Google Cloud 函数,我想生成缩略图。
虽然 Node.js 文档有一个使用 ImageMagick 的示例,但 Python 运行时没有这样的等效项。
考虑到性能的可接受方法是什么?Pillow-SIMD可以在云函数中工作吗?
或者我应该使用 App Engine 生成缩略图并使用图像服务?
您可以使用wand
ImageMagick 的绑定,并google-cloud-storage
在图像上传到存储桶后自动调整图像大小。
在requirements.txt
:
google-cloud-storage
wand
Run Code Online (Sandbox Code Playgroud)
在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())
Run Code Online (Sandbox Code Playgroud)
然后您可以使用该工具部署它gcloud
:
$ gcloud beta functions deploy make_thumbnail \
--runtime python37 \
--trigger-bucket gs://[your-bucket-name].appspot.com
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2337 次 |
最近记录: |