How do I view an image in dotnet interactive?

Viewed 238

I would like to view an image interactively in F# jupyter notebook similarly to how I can do this in python:

from PIL import Image

Image.open("image.png")

and that shows an image.

Is there a straightforward way to do this?

EmguCV, OpenCVSharp and ImageSharp all don't work together with the plotting libraries like Plotly.NET or XPlot to provide this functionality so I can't get something like matplotlib's pyplot.imshow.

1 Answers

In a notebook, you can display an image like this:

#!fsharp

// Load iamge
let data = File.ReadAllBytes("E:\\Downloads\\myImage.png");
// Convert so we can display it as HTML
let b64 = Convert.ToBase64String(data);
HTML($"<img src=\"data:image/png;base64,{b64}\"></img>") // last call without ; gets displayed
// Alt, this one has a semicolon: 
// display(HTML($"<img src=\"data:image/png;base64,{b64}\"></img>"));
Related