Display array as image in Pluto

I’m pretty new to Julia and Pluto so this is my first stumbling block. How do I display an array as an image in Pluto? Say I have an 8x8 array of Float64 that I want to display as a gray scale image.

The JuliaImages documentation have screenshots of it being done in a Jupyter notebook but I can’t get it to work in my Pluto notebook.

2 Likes

Just transform it to Colors should work. Pluto doesn’t work on my machine so I can’t test but I assume sth like

mat = rand(8,8)
using Colors
Gray.(mat)

should work.

1 Like

Unfortunately that only result in the following MethodError in Pluto:

Failed to show value:
MethodError: no method matching save(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Array{ColorTypes.Gray{Float64},2}, ::Pair{Symbol,ImageShow.var"#14#16"})
Closest candidates are:
save(::IO, ::S; compression_level, compression_strategy, filters, palette) where {T, S<:Union{AbstractArray{T,3}, AbstractArray{T,2} where T}} at H:\Users\Anders Dahnielson\.julia\packages\PNGFiles\Sgsmw\src\io.jl:278
save(!Matched::String, ::S; compression_level, compression_strategy, filters, palette) where {T, S<:Union{AbstractArray{T,3}, AbstractArray{T,2} where T}} at H:\Users\Anders Dahnielson\.julia\packages\PNGFiles\Sgsmw\src\io.jl:246

1. handle_error(::MethodError, ::FileIO.Stream{FileIO.DataFormat{:PNG},IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}})@error_handling.jl:82
2. handle_exceptions(::Array{Any,1}, ::String)@error_handling.jl:77
3. #save#31(::Base.Iterators.Pairs{Symbol,ImageShow.var"#14#16",Tuple{Symbol},NamedTuple{(:mapi,),Tuple{ImageShow.var"#14#16"}}}, ::typeof(FileIO.save), ::FileIO.Formatted, ::Any)@loadsave.jl:235
4. #show#13(::Int64, ::Int64, ::Function, ::typeof(show), ::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("image/png")}, ::Array{ColorTypes.Gray{Float64},2})@showmime.jl:43
5. show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("image/png")}, ::Array{ColorTypes.Gray{Float64},2})@showmime.jl:28
6. #show_richest#18(::Bool, ::typeof(Main.PlutoRunner.show_richest), ::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any)@PlutoRunner.jl:359
7. show_richest(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any)@PlutoRunner.jl:330
8. #sprint_withreturned#17(::IOContext{Base.PipeEndpoint}, ::Int64, ::typeof(Main.PlutoRunner.sprint_withreturned), ::Function, ::Array{ColorTypes.Gray{Float64},2})@PlutoRunner.jl:302
9. format_output(::Any)@PlutoRunner.jl:242
10. formatted_result_of(::Base.UUID, ::Bool)@PlutoRunner.jl:53
11. top-level scope@none:1

Weird, that worked for me. Did you try opening up a blank notebook and just running those three commands?

Yes, weird. It worked now for me too in a blank notebook.

It worked for me only importing Colors, but I got errors if I imported Images first so there might be some kind of conflict there.

1 Like

For large image, Colors does not work too

┌ Warning: Output swatches are reduced due to the large size (400×400).
│ Load the ImageShow package for large images.
└ @ Colors ~/.julia/packages/Colors/kc2v8/src/display.jl:148

Building on the suggestion by @mkborregaard - you need to have Images and ImageMagick in your environment to show png images:
https://github.com/JuliaIO/ImageIO.jl/issues/6

begin
import Pkg
Pkg.activate(mktempdir())
Pkg.add(["Images", "ImageMagick"])
using Images
end
mat = rand(8,8)
Gray.(mat)

Something simple, requiring no imports:

Usage is just like:

(Credit due to fonsp: this is just slightly adapted from https://github.com/fonsp/pluto-notebooks/blob/master/svd-video.jl, which I think was used in the JuliaCon 2020 Pluto talk. It was hard to find again so figured I’d mention it here.)

2 Likes