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) ?
models.py
views.py
display image
Templates
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 thedb.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:
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.