class TestImageHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
if upload_files:
blob_info = upload_files[0]
string = process_img(blob_info)
blobstore.delete(blob_info.key()) # optional: delete file after import
self.response.out.write('<img alt = "" src = "%s" >' % string)
def process_img(blob_info):
blob_reader = blobstore.BlobReader(blob_info.key())
data = blob_reader.read()
return "data:image/%s;base64,"%blob_info.filename.split('.')[1]+base64.b64encode(data)
Google App Engine Base64 Encode image from blobstore
-
19-10-2022 - |
Question
I am making a webapp that could accept user uploaded images and convert that to base64.
class TestImageHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
if upload_files:
blob_info = upload_files[0]
process_img(blob_info)
blobstore.delete(blob_info.key()) # optional: delete file after import
I plan to process the image in the process_img procedure, only to find that i do not know how to retrieve the image data from the blobstore and encode that.
No correct solution
OTHER TIPS
class TestImageHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
data_to_64 = re.search(r'base64,(.*)', upload_files).group(1)
decoded = data_to_64.decode('base64')
file_name = files.blobstore.create(mime_type='image/png')
with files.open(file_name, 'a') as f:
f.write(decoded)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow