I basically need to do this , but in Python instead of Javascript. I get the base64 encoded string from socketio connection, convert it to uint8 and work on it, then I need to convert it to base64 string so that I can send it.
So, up to this point I have it (I get the data dictionary from socketio server):
import pickle import base64 from io import BytesIO from PIL import Image base64_image_string = data["image"] image = Image.open(BytesIO(base64.b64decode(base64_image_string))) img = np.array(image)
How can I cancel this process to go from img to base64_image_string ?
UPDATE:
I solved this as follows (continued from the code snippet above):
pil_img = Image.fromarray(img) buff = BytesIO() pil_img.save(buff, format="JPEG") new_image_string = base64.b64encode(buff.getvalue()).decode("utf-8")
Somewhat vaguely, new_image_string not identical to base64_image_string , but the image obtained from new_image_string looks the same, so I'm satisfied!
source share