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

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

问题描述

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

  img = images.Image(img_data)
#基本上,我们只是想确定它是一个PNG
#因为我们没有一个好方法来通过API确定图像类型
#但是如果你的API抛出一个异常
#不要做任何转换,所以继续使用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)

图片(data = png_data,
thumbnail_data = thumbnail_data).put()

这段代码对我来说非常混乱,但对png起作用。然而,我应该怎么做才能存储所有最常见的格式(jpg,gif,tiff等)? 解决方案

快速回答



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

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






存储图像

为了简单地将图像存储在数据存储中,使用 db.BlobProperty() Line 85 )。

因为类型 db.BlobProperty 类型本身不是图片,但可以存储任何二进制数据,需要一些纪律;没有简单的方法来以编程方式强制执行仅限图片的约束。幸运的是,这意味着您可以存储任何类型的数据,包括 .jpg .gif ,<$除了 .png 格式之外,还可以包含 .tiff 等文件。



您可能想要像在示例中那样为模型创建一个新类,并存储文件所需的某些元数据(名称,文件类型等) ,除了图像的二进制数据。你可以在第65行在你链接的例子。



将图像存储在 BlobProperty ,你需要使用 db.put()来保存数据。这与任何类型都是一样的。查看从开始的代码在你链接到的示例代码中,第215行代表了


$ b

操作图像

如果您必须操作图片,则可以使用图片API 包。从 Images API概述,我们可以看到以下内容:
$ b


该服务接受JPEG,PNG,GIF(包括动画GIF),BMP,TIFF和ICO格式的图像数据。 / p>

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


即使技术上可以在数据存储中存储任何类型,但如果使用此API来操作图像,则有效的输入和输出typer是有限的。


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()

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) ?

解决方案

The quick answer

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

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.


Storing images

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).

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.

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.

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.


Manipulating images

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:

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

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.

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天全站免登陆