从 AppEngine 提供 GIF (Python)

Serve GIF from AppEngine (Python)

我可以从我的 python 应用引擎应用程序提供图像 (jpeg),使用内置图像 API 没有问题,如下所示:

result = img.execute_transforms(output_encoding=images.JPEG)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(result)

但这不适用于 GIF。以下

result = img.execute_transforms(output_encoding=images.GIF)
self.response.headers['Content-Type'] = 'image/gif'
self.response.out.write(result)

结果 Output encoding type not in recognized set frozenset([0, 1, 2])

查看 documentation(靠近顶部),我发现:

JPEG = images_service_pb.OutputSettings.JPEG
PNG = images_service_pb.OutputSettings.PNG
WEBP = images_service_pb.OutputSettings.WEBP
BMP = -1
GIF = -2
ICO = -3
TIFF = -4

OUTPUT_ENCODING_TYPES = frozenset([JPEG, PNG, WEBP])

这是否意味着 python 图片 api 当前不支持服务 GIF?如果是这样,还有其他方法可以从 python 应用引擎应用提供 gif 图像吗?

Does this mean that Serving GIFs is currently not supported by the python images api?

是的。通过实际产生您遇到的错误的代码片段确认,在您引用的同一文档页面的下方,在 execute_transforms_async():

    if output_encoding not in OUTPUT_ENCODING_TYPES:
      raise BadRequestError("Output encoding type not in recognized set "
                            "%s" % OUTPUT_ENCODING_TYPES)

If so, is there another way to serve gifs from a python app engine app?

是的,您可以改用 GAE 提供的 PIL library(GAE 中只有 1.1.7 版本可用)。

来自 Using get_serving_url()(强调我的):

In addition to the Images API, you can also use the transforms provided in the Python Imaging Library (PIL) in your Python 2.7 app. To do this, declare the library in the libraries section of the app.yaml file. However, if you wish to use PIL in your local environment (using the development server) you must also download and install PIL or pillow locally.

The get_serving_url() method allows you to generate a fixed, dedicated URL for an image that is stored in Blobstore.

注意:我实际上并没有尝试这个,答案完全基于文档。