Idan Moshe Idan Moshe - 1 month ago 9
Python Question

Convert (screenshot captured) image to base 64 without saving to png/jpeg in python

I saw a lot of q&a about converting images to base 64 string from image, all of them involving saving the image physically on pc.
What I want is convert the image to base 64 string without the need to save the image physically.

How can I do it with

pyscreenshot
or
ImageGrab
?

Answer
buffer = io.BytesIO()

im=ImageGrab.grab()
im.save(buffer, format='PNG')
im.close()

b64_str = base64.b64encode(buffer.getvalue())