How to create matrix of rgb values from a plot object

I’ve been trying to go from a plot to a matrix of rgb values for a while now and the best solution I could come up with makes use of saving the plot as png and then loading it as an image. This is of course a very dirty workaround so I’m wondering if there is a more neat, direct way to do this?
Here is a script that does what I want (in a very ugly way)

using Plots, Images

function main()
    x = 1:100
    y = sin.(x)
    plot(x, y)
    fn = "myPlot.png"

    # I'm looking for a function which has the same result as the two steps below that doesn't require saving to file.
    savefig(fn)
    myImage = load(fn)
end

main()

Fyi, Tim Holy seems to recommend exactly that in another post.

I would expect that savefig() (and png() since that is essentially the same function when saveing to png) would need to do exactly what I would like to do in order to save a plot as a png file. So it seems to me that this workaround is not necessary.
I briefly looked at the inner workings of png() but was not able to find out exactly how it works. Maybe someone with more experience on the implementation of savefig() or png() would know how this image is created and whether that method can also be used in other code without having to immediately save the image.

FWIW, this seems to do the same thing but avoids saving a file to disk:

using FileIO, Plots; gr()
p = plot(sin)
io_buf = IOBuffer()
show(io_buf, MIME("image/png"), p)
# io_buf.data

myImage = load(io_buf)  # Array{RGB{N0f8},2} with eltype RGB{N0f8}
plot(myImage, axis=nothing)
2 Likes

Awesome! Thank you so much!

At the same time I also found a different solution based on this stackoverflow question which in my opinion is less clean than the one proposed by @rafael.guerra. I will post the solution here anyway because it might be relevant for some people.

using Images
import PyPlot; const plt = PyPlot

function figToImage(fig)
    fig.canvas.draw()
    figureSize = round.(Integer, fig.get_size_inches() .* fig.dpi)
    invalidRGBString = fig.canvas.tostring_rgb()

    io = IOBuffer()
    write(io, invalidRGBString)
    UIntRGBVector = take!(io)

    pixelvalues = permutedims(reinterpret.(N0f8, reshape(UIntRGBVector, 3, figureSize...)), [1, 3, 2])

    return dropdims(mapslices(x -> RGB(x...), pixelvalues, dims=[1]), dims=1)
end

function main()
    x = 1:10
    y = sin.(x)
    fig = plt.figure()
    p = plt.plot(x, y)

    display(figToImage(fig))
    return
end

main()