Fri Aug 17, 2018 10:38 pm
Login Register Lost Password? Contact Us


Returning png data from embedded python

Comments and questions related to the Enterprise Control Language

Tue Dec 05, 2017 2:24 pm Change Time Zone

I'm trying to return an image from my embedded python. Here is my code:
Code: Select all
IMPORT Python;
DATA ReadAndOutputPngFile() := EMBED(Python)
    from imageio import imread
    import numpy as np

    output_index=65
    png=imread('class%s-maximal-image.png' % (output_index))
    return png
ENDEMBED;

OUTPUT(ReadAndOutputPngFile());


When I attempt to execute this, I get the following error message:
Code: Select all
Error: Error: 0: pyembed: type mismatch - bytearray expected


What must my return data type?
tlhumphrey2
 
Posts: 250
Joined: Mon May 07, 2012 6:23 pm

Wed Dec 06, 2017 1:48 pm Change Time Zone

Hi Tim,

I do not work with Python, but I found some interesting links:

https://stackoverflow.com/questions/7380460/byte-array-in-python

https://www.w3resource.com/python/python-bytes.php

I guess the compiler doesn't like DATA as the return value, maybe try a SET OF UNSIGNED1 ?

Regards,

Bob
bforeman
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 975
Joined: Wed Jun 29, 2011 7:13 pm

Tue Dec 19, 2017 4:31 pm Change Time Zone

With the help of Gordon Smith, I got this to work. But I had to change to platform 6.2.16-1. The following code displays the image as output to the workunit:
Code: Select all
IMPORT Python;
STRING ReadAndOutputPngFile() := EMBED(Python)
    import base64
    pngfile='Eigen_Silly_Professor_64x64.png';
    encoded = base64.b64encode(open(pngfile, "rb").read())
    return encoded
ENDEMBED;

img:=ReadAndOutputPngFile();
OUTPUT(DATASET([{'<img src="data:image/png;base64,' + img + '" />'}], {STRING base64Image__html}));
tlhumphrey2
 
Posts: 250
Joined: Mon May 07, 2012 6:23 pm


Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest

cron