data URI

ShellCollector發表於2019-03-02

 

https://stackoverflow.com/questions/54198488/how-to-convert-canvas-url-to-image-numpy-array-with-flask

 

https://stackoverflow.com/questions/19395649/python-pil-create-and-save-image-from-data-uri

 

import base64
from io import BytesIO

@app.route('/send_pic',methods=['GET','POST'])
def button_pressed():
    print("Image recieved")
    data_url = request.values['imageBase64']
    # Decoding base64 string to bytes object
    img_bytes = base64.b64decode(data_url)
    img = Image.open(BytesIO(img_bytes))
    img  = np.array(img)
    cv2.imshow(img)
    cv2.waitKeys(0)
    cv2.destroyAllWindows()
    return ""

 

from binascii import a2b_base64

data = 'MY BASE64-ENCODED STRING'
binary_data = a2b_base64(data)

fd = open('image.png', 'wb')
fd.write(binary_data)
fd.close()

相關文章