Skip to content

Instantly share code, notes, and snippets.

@ItsCosmas
Created Feb 4, 2020
Embed
What would you like to do?
Upload Base_64 Image File with Python Flask via REST API
from flask.views import MethodView
from flask import jsonify, request, abort
import uuid
import os
from io import BytesIO
from PIL import Image
import base64
class UploadAPI(MethodView):
def __init__(self):
if not request.json:
abort(400)
def post(self):
image_data = request.json['data']
extension = request.json['extension']
image_data = bytes(image_data, encoding="ascii")
image_name = str(uuid.uuid4()) + extension
image = Image.open(BytesIO(base64.b64decode(image_data)))
image_directory = os.path.join(os.getcwd(), 'static', 'upload', 'image','')
file_path = image_directory + image_name
# save image to file system
image.save(file_path)
response = {
"message": "Image Uploaded",
"body": {
"image_id": image_name,
}
}
return jsonify(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment