Machine Learning in Elixir B1, page 55. cannot open image

Hey @sehHeiden,

That’s because StbImage.read_file!() expects an absolute path to the image file.

# Expects
StbImage.read_file!("/path/to/Cat.jpg")

Assuming it’s in the same folder as your Livebook .livemd, we’ll need to figure out the current directory relative to your .livemd & image are in.

We do that via the following:

  • __DIR__: Returns the absolute path of the directory of the current file as a binary.
  • Path.join: Joins two paths.

Putting it together

__DIR__
|> Path.join("Cat.jpg")
|> StbImage.read_file!()
|> StbImage.resize(256, 256)
|> StbImage.to_nx()
|> Nx.dot(invert_color_channels)
|> Nx.as_type({:u, 8})
|> Kino.Image.new()

That should locate the file correctly & invert the image:

Hope it helps!