M
M
maryaturova2021-08-03 18:35:08
Flask
maryaturova, 2021-08-03 18:35:08

Why is there an error when getting an image in base64?

Dear! Tell me what I'm doing wrong?

Send image in base64 format
import requests
img = 'iVBORw0KGgoAAAANSUhEUgAAAFgAAAAoCAYAAAB6tz31AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAM9ElEQVRoge2af3QV1bXHP7kJwyWGGJMQHB6X3IV5MFKkPAqoYGEWKCvVVgXhFZRof6xWEavlWXRRHrpYLsvCLkQWzx884Pkj/CiPCljRprWGofwQFTFFHgzIj4FJGQiXEEJ+OdzcvD/OmTC53CRcAtSy3GvNmsw5+5y9z/fss/c+Oxe+oWbSVN35R+vQYbrYRVzuxV8V4Hp0qUHuKDhtjb+qgL8Y6ijomqo77fH+Q0D+Ou9sMrp9ndeRULnLqfDXGowrQVcCgKtFxlVPlyNgfrMxkjRVb7wMc3YcXG+SZCf7Ou7sldIp5UIZpUJPA4uAl4ATQMT37Dcdo7KNsZiOoXZU4WRJU3XnSsqNX+sFAywHLwTGt8GyH1hoOsb/+gX6F6ip+gBgI/AEUAlUIDboqOkYDcnok4TeFwRyRzbDG9tRgFcCOnAPkAHk+p6bgYFAAFhmOsb0uLEKUAyMkE15CHA9coHtwHzTMTb7xg0AZgPvmY6xJBl9k6HWwL0Q0NviSRbgD4DepmPc0Ep/T+BNoB8wxXSMdb6+OcCPgG3AagSg3uZ0AwoQGxQDJnkga6oeBL4ELNMxvtuOfr2Bl4HdpmM82QpPLvB/pmN0T9CX0JV1xLLTkuTPQxxnACzXHg8MAU4hjnvlxne3LX3hiVcXkJLyJLDON/ZXQBkwwXSMaCKlNVUfAawCfgFsBjAdo0FT9TJgsKbqeaZj+K0+niYiNmlTok5N1YcC7wDbfEf6QWA4wvW5iYDsiA9PFuBsBEgefRsY42cY+YNbeH/FBuXQniO3fVGx592uWRmLCvOLXkdszCfx4Fqu3Q/oDQSBqrv7/rSqKRbTLNcOhpWQ55O3AIOBW4A/JFJMU/UA8EP5+ftW9H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IEDKAgj37bkdKA4roe3tTLcD6ZOB1fHWDnyCADgd2OxtuD+uaKru8WYDNWEl5PonMB3D0FT9xjNVNR8hyqa3cy6nbxPgVumf6ccilms/Zbn2Isu18+L7Bmt3Hbdc+zPLtT+3XFtvbR5N1R3LtTdZrv1OWzrIEuXnvid4kcu5PL+evJI/vvb6LNce9dhj/3nacu22wFUkYK+fP1NLslx7peT9KJk1tEuXyqov9em4gB9Qt/tbNAlyf+m22pzXcu1hEuD3ktf2KqVLfXIs1+7t34yOyvmGkqBvQL6C9P81e8ESIeGYGAAAAABJRU5ErkJggg=='

res = requests.get('http://127.0.0.1:5000/ocr?img='+img)
answer = res.text
print(answer)

Accept it on Flask
from PIL import Image
import io

app = Flask(__name__)

@app.route('/ocr', methods=['GET'])
def ocr ():
    img = request.args.get('img')
    
    im = Image.open(io.BytesIO(base64.b64decode(img)))
    im.save('1.png')
    im.close()
    return 'ok'

if __name__ == "__main__":
    app.run()

I am getting an error:
610961bad5a7c308383731.png

im = Image.open(io.BytesIO(base64.b64decode(img)))
File "C:\Users\kdw22\AppData\Local\Programs\Python\Python36\lib\base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

There are no unicode characters. The picture is sent as is.
At the same time, if you copy base64 and paste it into the browser, the picture opens.
What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maryaTurova, 2021-08-03
@maryaturova

I'll answer myself:
urllib.parse.quote_plus()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question