I am trying to manipulate a "test.jpg" image in Elixir by using Erlang wxImage library, but I am getting an error. I do not know how to convert the array/const output to a list so I can use it in Elixir.
Also I do not know why there is a clause function error when the syntax seems to be alright?
defmodule Imedit2 do
def readimg(image) do
{:ok, _file} = File.open("happy737.txt", [:write])
IO.puts("hi there")
_output =
image
|> File.read!()
|> :wxImage.getData()
|> to_charlist()
# IO.puts(is_list(output))
# IO.puts(is_tuple(output))
# IO.binwrite(file, output)
# File.close(file)
end
end
iex(58)> Imedit2.readimg("test.jpg")
hi there
** (FunctionClauseError) no function clause matching in :wxImage.getData/1
The following arguments were given to :wxImage.getData/1:
# 1
<<255, 216, 255, 226, 2, 28, 73, 67, 67, 95, 80, 82, 79, 70, 73, 76, 69, 0, 1,
1, 0, 0, 2, 12, 108, 99, 109, 115, 2, 16, 0, 0, 109, 110, 116, 114, 82, 71,
66, 32, 88, 89, 90, 32, 7, 220, 0, 1, 0, 25, ...>>
gen/wxImage.erl:405: :wxImage.getData/1
lib/imedit2.ex:5: Imedit2.readimg/1
I had a play around with
:wxImage
and I found a couple of problems with your code::wx.new()
to to initialize wx before any of the:wxImage
functions will work.getData/1
should be the image handle, not the binary file data. From the docs:And for
getData/1
:So you can do it like this:
But beware that the
bin_to_list/1
call is slow, and I don't think you need it anyway. You probably want to stop at:wxImage.new()
, keep the handle in a variable, and use that to call whatever other:wxImage
functions you need.