将 jpg、gif、png 等存储在 gae-datastore [英] Store jpg, gif, png, etc it gae-datastore

查看:12
本文介绍了将 jpg、gif、png 等存储在 gae-datastore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个 示例 关于如何在数据存储中存储 png:

I found an example on how to store png in datastore:

  img = images.Image(img_data)
  # Basically, we just want to make sure it's a PNG
  # since we don't have a good way to determine image type
  # through the API, but the API throws an exception
  # if you don't do any transforms, so go ahead and use im_feeling_lucky.
  img.im_feeling_lucky()
  png_data = img.execute_transforms(images.PNG)

  img.resize(60, 100)
  thumbnail_data = img.execute_transforms(images.PNG)

  Picture(data=png_data,
          thumbnail_data=thumbnail_data).put()

这段代码让我很困惑,但它适用于 png.但是,我应该怎么做才能存储所有最常见的格式(jpg、gif、tiff 等)?

This code is very confusing to me, but it works for png. However, what should I do to be able to store all most common formats (jpg, gif, tiff, etc) ?

推荐答案

快速解答

您可以在模型中使用 db.BlobProperty() 存储任何文件类型的二进制数据.

The quick answer

You can store binary data of any file type by using db.BlobProperty() in your model.

如果您使用Image API 来操作图像数据,您只能输入.jpg.png.gif.bmp.tiff.ico 类型,并输出到 .jpg.png.

If you use the Image API to manipulate the image data, you're limited to inputting .jpg, .png, .gif, .bmp, .tiff, and .ico types, and outputting to either .jpg or .png.

要简单地将图像存储在数据存储中,请在模型中使用 db.BlobProperty(),并让它存储图片的二进制数据.这是数据在您链接到的示例代码中的存储方式(请参阅 第 85 行).

To simply store the images in the data store, use db.BlobProperty() in your model, and have this store the binary data for the picture. This is how the data is stored in the example code you linked to (see Line 85).

因为db.BlobProperty类型本身不是图片,但可以存储任何二进制数据,所以需要一些纪律;没有简单的方法可以以编程方式强制执行仅限图片的约束.幸运的是,这意味着您可以将任何类型的数据存储在除了 .png 格式,如示例所示.

Because the type db.BlobProperty type is not a picture per se, but can store any binary data, some discipline is needed; there's no easy way to programmatically enforce a pictures-only constraint. Luckily, this means that you can store data of any type you want, including .jpg, .gif, .tiff, etc. files in addition to the .png format, as in the example.

您可能希望,正如他们在示例中所做的那样,为模型创建一个新类,并存储文件所需的某些元数据(名称"、文件类型"等),此外到图像的二进制数据.您可以在 您链接到的示例中的第 65 行.

You'll probably want to, as they have in the example, create a new Class for the Model, and store certain metadata ("name", "filetype", etc.) needed for the files, in addition to the image's binary data. You can see an example of this at Line 65 in the example you linked to.

要将图像存储在BlobProperty 中,您需要使用db.put() 来保存数据;这与任何类型相同.请参阅从 您链接到的示例代码中的第 215 行.

To store the image in the BlobProperty, you'll want to use the db.put() to save the data; this is the same as with any type. See the code starting on Line 215 in the example code you linked to.

如果您必须处理图像,可以使用 Images API 包.从图像 API 概述我们可以看到以下内容:

If you have to manipulate the image, you can use the Images API package. From the Overview of the Images API we can see the following:

该服务接受 JPEG、PNG、GIF(包括动画 GIF)、BMP、TIFF 和 ICO 格式的图像数据.

The service accepts image data in the JPEG, PNG, GIF (including animated GIF), BMP, TIFF and ICO formats.

它可以返回 JPEG 和 PNG 格式的转换图像.如果输入格式和输出格式不同,服务会在执行转换之前将输入数据转换为输出格式.

It can return transformed images in the JPEG and PNG formats. If the input format and the output format are different, the service converts the input data to the output format before performing the transformation.

因此,即使您在技术上可以在数据存储中存储任何类型,但如果您使用此 API 来操作图像,则有效的输入和输出类型器会受到限制.

So even though you can technically store any type in the datastore, the valid input and output typer are limited if you're using this API to manipulate the images.

这篇关于将 jpg、gif、png 等存储在 gae-datastore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆