python-3.x - 使用 Python37 运行时使用 Cloud Functions 生成缩略图
问题描述
我有一个由 Firebase 存储触发的 Google Cloud 功能,我想生成缩略图。
虽然 Node.js 文档有一个使用 ImageMagick 的示例,但 python 运行时没有这样的等价物。
牢记性能的可接受方法是什么?Pillow-SIMD会在云功能中工作吗?
或者我应该去 App Engine 生成缩略图并使用图像服务?
解决方案
您可以使用wand
ImageMagick 的绑定,以及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
推荐阅读
- arrays - 尝试从 SwiftUI 视图中的嵌套数组调用
- javascript - 为我的原型使用“Swiper”JS 库时的 CSS 挑战
- scala - 在 Scala 中,是否可以通过迭代 ListMap 将部分函数的集合构建成一个函数?
- firefox - Chart.js 的跨浏览器支持
- jquery - Django:模型和视图以及 jQuery DataTables,hello world 示例
- firebase - 重新导航后重新创建流
- docker - Docker 列表容器 - Gitlab Runner
- python - 如何更改 TensorFlow 执行加法的方式?
- c++ - C++中链表实现的问题
- angular - 当在我的 Angular 8 反应形式中选择了不正确的答案时,如何突出显示正确的答案?