I am been really struggling on this problem and thought it was time to reach out. I need to convert a KNIME Binary Object (an image binary return from an API) to a Base64 string so I can use it in PowerBI to display an image in a dashboard.
I have tried everything I can think of to get the Binary to Base 64 and I have been unsuccessful.
I need to end up with a string output something like this:
My data from the GET:
My data after Binary To String with ASCII set as the Decode:
I am not very proficient in JAVA and wrote this to try to convert the Binary to a Base64 string:
The JAVA Snippet outputs this:
I tried in Python as well and ended up with a similar result:
import pandas as pd
input_table_1['body_encoded'] = [base64.b64encode(x.encode('ASCII', 'strict'))
for x in input_table_1.body]
output_table_1 = input_table_1
The output from my Python Script
It just doesn’t look like the Base64 I’m getting from the online conversion tools. I am open to a JAVA, R or Python solution to this if anyone has any ideas/suggestions. I really appreciate it!
Thanks for sharing I did not see this! I will use the Palladian node which converts it to the format I need.
Hi @TardisPilot , the encoding nodes from Palladian are the best I find. Very user friendly, simple to use.
At one point, I had to find an alternative, as our Knime Server did not have the Palladian extension. I used a Java Snippet, and this is what worked for me:
out_Base64EncodedString = org.apache.commons.codec.binary.Base64.encodeBase64String(c_Content.getBytes());
Thanks for sharing! I would definitely like to come up with a native KNIME solution for sure since we can’t always install external extensions.
I ran the JAVA code and it generated this:
Whereas the Palladian node generates this (which works perfectly):
I’m not an expert in encoding/decoding data so I wonder what the difference is here and how to get the JAVA to output like the Palladian node.
Hi @TardisPilot , I’m not an expert in this either unfortunately. It looks like the Java code I gave you is giving the same results that you got through Python.
A bit of background on the code I provided, this is what I used convert AWS ClientID+ClientSecret to Base64 when requesting an AWS Cognito token, so it’s basically converting a string to Base64.
I tried different functions in Java back then, but as you are doing too, I converted some string online and via PHP which were giving me the correct values first, and compared with the different trial and error I was doing, until I tried the one I gave you which was giving me the expected values.
I think in your case, you are trying to convert binary to Base64?
Is it possible to share your input data to be encoded so I can play around and see if we can get to the correct solution without the extension?
Edit: And also the expected result of the encoded data?
I’ll put something together and post it here. Thanks!
Hey @bruno29a ,
Here is a workflow I put together to help tackle this:
My ultimate goal is to find a native KNIME method to use (ie JAVA snippet) that generates the same Base64 string that the Base64 Encoder node does.
Hi @TardisPilot , I got it to work!
As I suspected, the code that I gave you was to convert a string to Base64, while you want to convert a binary object to Base64, so it needed some modification.
Taking a step back, the
Base64.encodeBase64String() function takes a Byte array (
byte), and in case of a string, we simply used the
.getBytes() method like this
However, with the binary object, or more precisely an InputStream, you can’t use this method, but we still need to convert to a Byte array (most of the Base64 methods take a Byte array as argument).
I used the toByteArray() method from the IOUtils libraries (org.apache.commons.io.IOUtils), so you will need to have the “apache commons io” jar files to use it:
This is what the Java Snippet looks like in the end:
And you point to the jar file like this:
And here is the result of the Base64 encoded (top part from the Base64 Encoder node, bottom part from Java Snippet):
And here’s the workflow: Image_Base64_Test.knwf (760.9 KB)
I added it as a new node:
Hey @bruno29a ,
This is fantastic! I really appreciate you taking the time to document this like you did. I think it’s nice to have a native KNIME solution like this. Thank you again!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.