Im new to stackoverflow and this will be my first quastion. I will try to explain my problem as good as I can.
Im doing python code for a server. Where I load an image and do a lot of image processing on it. It ends up as a np.array binary mask (that can be converted to an Image object if needed). Now I need to encode this image in base64, to send it to client with json. On the client side it's not python code and it have to be decoded and readable as a normal image format. I have only find examples for encoding a image that is loaded directly from a file. So a solution is to save it as a temp image, then load this image and encode it. But this is very ugly. There must be a way to directly encode it and specify what image format you want to encode it as?
test_img = Image.fromarray(back_seg*255) # from np.array output = StringIO() test_img.save(output, format='JPEG') im_data = output.getvalue() data_url = base64.b64encode(im_data) g = open("out.JPEG", "w") g.write(data_url.decode('base64')) g.close()
– Daniel Apr 23 '13 at 09:06