How to convert tensor image from Generator to base64 for sending?

I have a flask server that I want to listen to JSON requests for Z input (working), generate an image using that vector (working), and then send the image using localhost to another app. It should be base64 encoded, as I understand it. How do I transform the tensor into this format?


@app.route("/json", methods=['GET', 'POST', 'PUT'])
def getjsondata():


    if request.method=='POST':
        print("received POST")

        data = request.get_json()

        #print(format(data['z']))
        jzf = [float(i) for i in data['z']]
        jzft = torch.FloatTensor(jzf)
        jzftr = jzft.reshape([1, 512])

        z = jzftr.cuda()
        c = None                   # class labels (not used in this example)
        trunc = 1
        img = G(z, c, trunc)
        img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)

        # this is how I was saving image to disk, but I would rather not save before sending
        # PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save('newtest.png')

        # this isn't working
        pil_image = PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
        encoded_string = base64.b64encode(pil_image)

        return encoded_string

I managed a solution

@app.route("/json", methods=['GET', 'POST', 'PUT'])
def getjsondata():

    if request.method=='POST':
        # print("received POST")

        data = request.get_json()

        #print(format(data['z']))
        jzf = [float(i) for i in data['z']]
        jzft = torch.FloatTensor(jzf)
        jzftr = jzft.reshape([1, 512])

        z = jzftr.cuda()
        c = None                   # class labels (not used in this example)
        trunc = 1
        img = G(z, c, trunc)

        #img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
        img = (img * 127.5 + 128).clamp(0, 255).to(torch.uint8)

        # turn into PIL image
        pil_img = transforms.ToPILImage()(img[0]).convert("RGB")
        #pil_img = PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
        pil_img.save('newtest.png')

        response = serve_pil_image64(pil_img)
        response.headers.add('Access-Control-Allow-Origin', '*')
        # response.headers.add('Content-Transfer-Encoding', 'base64')
        return response


    return 'OK'

def serve_pil_image64(pil_img):
    img_io = BytesIO()
    pil_img.save(img_io, 'JPEG', quality=70)
    img_str = base64.b64encode(img_io.getvalue()).decode("utf-8")
    return jsonify({'status': True, 'image': img_str})

1 Like