sehHeiden

sehHeiden

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

@seanmor5

I want to invert the cat image. Therefore I downloaded the file in the same folder as the livemd file for chapter 03 I created.
I use the code:


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

returns:

** (ArgumentError) could not open file
(stb_image 0.6.2) lib/stb_image.ex:247: StbImage.read_file!/2
/path_to_folder/ch03_harness_the_power_of_math.livemd#cell:4r2dpa25qaotbzseednwllnwqlrrvd6l:8: (file)

Hope you can help me out here.

First Post!

shawn_leong

shawn_leong

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!

Popular Pragmatic topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1132 25307 748
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New

Latest in PragProg

View all threads ❯