Using the requests library:
import base64
import requests
def get_as_base64(url):
return base64.b64encode(requests.get(url).content)
Answer from user94559 on Stack OverflowUsing the requests library:
import base64
import requests
def get_as_base64(url):
return base64.b64encode(requests.get(url).content)
Since requests is not an official package, I prefer to use urllib.
from urllib.request import urlopen
import base64
base64.b64encode(urlopen("http://xxx/yyy/abc.jpg").read())
How to convert decode base64 image , in url
Python requests base64 image - Stack Overflow
python - Encoding an image file with base64 - Stack Overflow
Converting a dataurl to numpy array
https://8c97-106-213-197-139.ngrok.io/mynsfw/predict?lnk=data:image/jpeg;base64,/9j/4AAQSkZ..................CESAf/2Q==
Link to the full request
When i get a request like this on my flask server , how do i get image out of "lnk".
If there is better way to receive the link to flask let me know that too , i can make those changes too.
What i have tried
bs64_rg=re.compile(r'data:image\/([a-zA-Z]*);base64,([^\"]*)')
# bts_io=BytesIO()
try:
url=(request.args.get('lnk'))
bs64=bs64_rg.search(url)
if bs64:
r=bs64.group(2)
r=base64.b64decode(r)
# bts_io.write(r)
# img=Image.open(bts_io.write(r))
# bts_io.truncate(0)
# bts_io.seek(0)
# img = Image.open(BytesIO(r))
img = image.img_to_array(image.load_img(BytesIO(r),target_size=(224,224))) / 255.My try , please ignore the lot of commented stuff
I think it's just
import base64
import requests
response = requests.get(url)
uri = ("data:" +
response.headers['Content-Type'] + ";" +
"base64," + base64.b64encode(response.content))
Assuming content-type is set.
This worked for me:
import base64
import requests
response = requests.get(url)
uri = ("data:" +
response.headers['Content-Type'] + ";" +
"base64," + base64.b64encode(response.content).decode("utf-8"))
I'm not sure I understand your question. I assume you are doing something along the lines of:
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
You have to open the file first of course, and read its contents - you cannot simply pass the path to the encode function.
Edit: Ok, here is an update after you have edited your original question.
First of all, remember to use raw strings (prefix the string with 'r') when using path delimiters on Windows, to prevent accidentally hitting an escape character. Second, PIL's Image.open either accepts a filename, or a file-like (that is, the object has to provide read, seek and tell methods).
That being said, you can use cStringIO to create such an object from a memory buffer:
import cStringIO
import PIL.Image
# assume data contains your decoded image
file_like = cStringIO.StringIO(data)
img = PIL.Image.open(file_like)
img.show()
import base64
from PIL import Image
from io import BytesIO
with open("image.jpg", "rb") as image_file:
data = base64.b64encode(image_file.read())
im = Image.open(BytesIO(base64.b64decode(data)))
im.save('image1.png', 'PNG')