<@U0CHHN4F4> another question, I’ve noticed and su...
# datascience
h
@ilya.chernikov another question, I’ve noticed and successfully reproduced the svg example
KotlinSample01.ipynb
in the repo. Is there a similar way to display instances of
java.awt.image
?
i
I had to refresh, how it works 🙂 Basically you’ll need to convert it to a mime format, maybe “image/bitmap” or something. The same way as it is done for svg in the sample:
Copy code
fun<T: Drawable> T.toSvg(sizeX: Double, sizeY: Double): Pair<String, Any> {
    val writer = DrawableWriterFactory.getInstance().get("image/svg+xml")

    val buf = ByteArrayOutputStream()

    writer.write(this, buf, sizeX, sizeY)

    return writer.mimeType to buf
}
It seems that java.awt.image default mime type is defined as
image/x-java-image
, so it may well be that you just have to return a pair consisting of that mime type string and the image itself. Or maybe it’s buffer.
h
does not work for me. Neither:
Copy code
import java.io.File
import javax.imageio.ImageIO

val pathToFile =  File("/Users/brandl/Downloads/Clipboard.png");
val image = ImageIO.read(pathToFile);
print(image)

resultOf("image/x-java-image" to image)
nor the buffer variant
Copy code
import java.io.ByteArrayOutputStream

val baos= ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos );
val imageInByte = baos.toByteArray();

resultOf("image/x-java-image" to imageInByte)
Finally I’ve tried a mime-aware writer, but also no luck:
Copy code
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

val pathToFile = File("/Users/brandl/Downloads/Clipboard.png")
val image = ImageIO.read(pathToFile)


// Draw the image on to the buffered image
val bimage = BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB)
val bGr = bimage.createGraphics()
bGr.drawImage(image, 0, 0, null)
bGr.dispose()

val buf = ByteArrayOutputStream()

val writer = ImageIO.getImageWritersByMIMEType("image/jpeg").next()
val ios = ImageIO.createImageOutputStream(buf)
writer.setOutput(ios)
writer.write(bimage)
ios.close()
        
resultOf("image/jpeg" to buf)
None of those give an error, but no image neither.
i
Seems you need base64-encode your image. The following works for me:
Copy code
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO
import java.util.Base64

val image = BufferedImage(100, 50,
        BufferedImage.TYPE_INT_ARGB)

val g2 = image.createGraphics()

for (x in 30..50)
    for (y in 20..30)
        image.setRGB(x, y, 0xf0f0f0)

val buf = ByteArrayOutputStream()

val writer = ImageIO.getImageWritersByMIMEType("image/jpeg").next()

val ios = ImageIO.createImageOutputStream(buf)
writer.output = ios
writer.write(image)
ios.close()
val b64img = Base64.getEncoder().encodeToString(buf.toByteArray())        
resultOf("image/jpeg" to b64img)
h
Indeed, this works just great. Thanks for the help.
Is it possible to register custom handlers in the notebook to auto-convert whatever object into displayable mime-typed representation? Basically to wrap the code from above to simple allow for image inlining with something like
ImageIO.read(File("test.png))
.