JPEG blob to image cell

  1. Is there a way to convert a binary blob cell with a JPEG blob into a KNIME image cell? (i.e. which shows the image data inline instead of blob chunk)

  2. It seems possible to create image cells through the Java Snippet node. But the only available type is ImgPlus, and I haven’t figured out its API. Any documentation or possibility to work e.g. with java.awt.image.BufferedImage?

Hi @qqilihq,

  1. There is a Binary Objects to PNGs node that does exactly what you describe for PNGs. We currently don’t have an equivalent for JPEGs.
  2. ImgPlus is part of the ImageJ2 project and a wrapper for ImgLib2 data structures which form the basis for KNIME Image Processing.

Are you stuck with JPEGs or could you also migrate to using PNGs?

Best,
Stefan

1 Like

In addition, you could use the Binary Objects to Files node to write the BLOBs to a temporary directory and open them via the Image Reader.

2 Likes

You can actually convert the JPEG bytestream to a PNG bytestream using the java ImageIO API. Without writing anything to disk. See the attached workflow for how to do it:

Binary JPEG to PNG.knwf (23.8 KB)

best,
Gabriel

2 Likes

Hey guys,

thank you for the very valuable suggestions and input!

For my specific case the combination of PNG to JPEG via the Java Snippet followed by a Binary Objects to PNGs seems the best fit and I’ll update my existing workflow.

What I actually did as a workaround in the meantime (probably the most absurd solution, but for my proof of concept workflow it was sufficient :blush: ): Create an SVG cell through a Java Snippet node which would contain the JPEG embedded within a data URL like so:

// base64-encode the JPEG
String imageBase64 = new String(Base64.getEncoder().encode(imageBytes));
String imageHref = String.format("data:image/jpeg;base64,%s", imageBase64);

StringBuilder b = new StringBuilder();
b.append("<?xml version=\"1.0\" standalone=\"no\"?>");
b.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
b.append("<svg width=\"128px\" height=\"128px\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
b.append("<image xlink:href=\"" + imageHref + "\" x=\"0\" y=\"0\" height=\"128px\" width=\"128px\"/>");
b.append("</svg>");

out_userimagesvg = b.toString();

Best,
Philipp

2 Likes

Nice hack :sunglasses:!