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
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
.
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.
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.
class Profile(db.Model):
avatar=db.BlobProperty()
if(self.request.get):
image = self.request.get('MyFile')
if image:
mime=self.request.POST['MyFile'].type
mime=mime.split('/')
icon_image = db.Blob(images.resize(image,460,460))
prof.avatar = db.Blob(icon_image)
if mime[1]== 'jpeg' or 'jpg' or 'gif' or 'png':
prof.put()
class disp_image(webapp.RequestHandler):
def get(self):
if profile.avatar is not None:
image = view_profile.avatar
self.response.headers['Content-Type'] = "image/png"
return self.response.out.write(image)
<img id="crop" src='/module/disp_image' alt="profile image" >