简体   繁体   English

在上传到Google App引擎之前调整图片大小

[英]Resize Image before Upload to Google App engine

I would like to be able resize an image blob before I save it to a database with google app engine 我希望能够在使用谷歌应用引擎将其保存到数据库之前调整图像blob的大小

from google.appengine.api import images
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import db

class ImageModel(db.Model):
    image1 = blobstore.BlobReferencePropert(required = True)


class UploadImageHandler(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
            upload_files = self.get_uploads('image1')
            blob_info = upload_files[0]
            blob_key = blob_info.key()
            img = images.Image(blob_key = blob_key)
            img.resize(width = 500, height = 500)

            i = ImageModel(image1 = img)
            i.put()

Of course this doesn't work since img is no longer a blob. 当然这不起作用,因为img不再是一个blob。 How to I convert the image back into a blob and then upload to database. 如何将图像转换回blob然后上传到数据库。 I don't want to serve the image dynamically and resize. 我不想动态地提供图像并调整大小。 I need to have a resized image in the database. 我需要在数据库中有一个调整大小的图像。

now blobstore support write file directly https://developers.google.com/appengine/docs/python/blobstore/overview#Writing_Files_to_the_Blobstore 现在blobstore支持直接写文件https://developers.google.com/appengine/docs/python/blobstore/overview#Writing_Files_to_the_Blobstore

so you can have something like this. 所以你可以有这样的东西。

# resize your img

# create file
file_name = files.blobstore.create(mime_type='application/octet-stream')
with files.open(file_name, 'a') as f:
    f.write(img)    

# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)

# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM