6

This is a trivial question, of course, but I'm having a hard time finding a working answer.

Given a matrix A ( either 2d for grayscale, or 3d for RGB) how to save it on the disk as an image file using Julia?

I have an old code written in 2016 when I would use

save("filename.png",Images.colorim(matrix_A))

Now this seems to be gone for good.

Hayk
  • 487
  • 2
  • 8
  • 15

1 Answers1

12

you could use colorview to view your raw matrix as an image and then save it.

julia> using Images

julia> save("gray.png", colorview(Gray, rand(256,256)))

enter image description here

julia> save("rgb.png", colorview(RGB, rand(3,256,256)))

enter image description here

Gnimuc
  • 6,905
  • 2
  • 32
  • 45
  • 1
    With Julia v.1.1 and Images v0.17.3 this produces the error `ERROR: FileIO.File{FileIO.DataFormat{:UNKNOWN}}("gray.png") couldn't be recognized by FileIO.` – Mankka Apr 04 '19 at 06:00
  • 1
    @Mankka cannot reproduce it with ImageMagick backend – Gnimuc Apr 04 '19 at 06:06
  • I hade to use `save(File(format"PNG", "gray.png"), colorview(Gray, rand(256,256)))` to make it work. I wonder what difference in our systems produces this error... – Mankka Apr 04 '19 at 06:11
  • @Mankka probably, I only tested it on Windows. – Gnimuc Apr 04 '19 at 06:12