Skip to content

Instantly share code, notes, and snippets.

@ItsCosmas
Created February 4, 2020 09:00
Show Gist options
  • Save ItsCosmas/9452b3710f5329cf2974c1b05a0a27c3 to your computer and use it in GitHub Desktop.
Save ItsCosmas/9452b3710f5329cf2974c1b05a0a27c3 to your computer and use it in GitHub Desktop.
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