I need to get an image via a URL, and without saving the image locally base64 encode into a string which in then past as an api call via a URL. Im able to do it no problems if i can save the file locally with:
with open(photo.jpg, "rb") as file: image = [base64.b64encode(file.read()).decode("ascii")] After some research I thought I had found a way with the following of doing it without saving:
URL = 'http://www.w3schools.com/css/trolltunga.jpg' with urllib.request.urlopen(URL) as url: f = io.BytesIO(url.read()) image = base64.b64encode(f.read()).decode("ascii") however I get the following error:
Traceback (most recent call last): File "C:\Users\rich2\Documents\Python\get_image_stream\app3.py", line 13, in <module> image = base64.b64encode(img.read()).decode("ascii") File "C:\Users\Documents\Python\get_image\venv\lib\site-packages\PIL\Image.py", line 546, in __getattr__ raise AttributeError(name) AttributeError: read Im clearly missing something but cannot find a workable answer anywhere.
line 13? The code block presentsimage = base64.b64encode(f.read()).decode("ascii"), the trace shows fault found withimg.read().