Store jpg, gif, png etc. Gae-datastore

I found an example on how to store png in a data warehouse:

img = images.Image(img_data) # Basically, we just want to make sure it 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 store all the most common formats (jpg, gif, tiff, etc.)?

+2
source share
2 answers

Quick response

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

If you use the Image API to manage image data, you are limited to entering the types .jpg , .png , .gif , .bmp , .tiff and .ico and outputting to .jpg or .png .


Saving Images

To simply save images to the data warehouse, use db.BlobProperty() in your model and save this binary data for the image. This is how the data is stored in the code below (see Line 85 ).

Since the type of type db.BlobProperty is not in itself, but can store any binary data, some discipline is needed; There is no easy way for software to enforce restriction of images only. Fortunately, this means that you can store any type of data you want, including .jpg , .gif , .tiff , etc. Files in addition to the .png format, as in the example.

You will probably want, as in this example, to create a new class for the model and save the specific metadata ("name", "file type", etc.) necessary for the files, in addition to the binary image data. You can see an example of this on line 65 in the example you are attached to.

To save the image in BlobProperty , you want to use db.put() to save the data; it is the same as for any type. See Code starting with Line 215 for an example of the code you are attached to.


Image manipulation

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

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

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

Thus, although you can technically store any type in a data warehouse, the valid type of input and output is limited if you use this API to manage images.

+7
source

models.py

  class Profile(db.Model): avatar=db.BlobProperty() 

views.py

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

display image

 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) 

Patterns

 <img id="crop" src='/module/disp_image' alt="profile image" > 
+3
source

Source: https://habr.com/ru/post/1333818/


All Articles