0

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?

Daniel
  • 41
  • 3
  • 1
    Does [this previous answer of mine](http://stackoverflow.com/questions/16065694/is-it-possible-to-create-encodeb64-from-image-object/16066722#16066722) help? – Martijn Pieters Apr 23 '13 at 08:13
  • I think it helps, I tried it and then decoded the image back again do verify. But the output image looks abit wierd. But maybe my decodeing is the problem. The code looks like this: 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
  • Encode, to a `data:` URL, then paste that into your browser to verify the image. – Martijn Pieters Apr 23 '13 at 09:07
  • Thanks, that works. Sry for not finding that question myself. So how do I close this question? – Daniel Apr 23 '13 at 09:48
  • I've voted to close it as a duplicate, which pushes it into the review queue. – Martijn Pieters Apr 23 '13 at 10:17

0 Answers0