Skip to content

Useful Code Scraps

Dehann Fourie edited this page Aug 16, 2020 · 17 revisions

Plot image in Gadfly

https://github.com/GiovineItalia/Gadfly.jl/issues/288#issuecomment-462754808

Images and Bytes

using TestImages
img = testimage("mandrill")

# convert image
io = IOBuffer()
pngSm = Stream(format"PNG", io)
save(pngSm, img)
pngBytes = take!(io)

Alternatively, ImageMagick/FileIO can work through files for lazy convert:

# Lazy convert to PNG 
using FileIO
save("/tmp/test.png", img)

# Lazy convert to bytes
imgData = open(read, "/tmp/test.png")
# alternative
# imgData = Vector{UInt8}(undef, stat(f).size ÷ sizeof(UInt8))
# fid = open("/tmp/something.png", "r")
# read!(fid, imgData)
# close(fid)

# Lazy convert of PNG to image Array
rgb = ImageMagick.readblob(imgData);

using ImageView
imshow(rgb)