I have a function f(x)
that receives some input that might be a plot or some form of image. If that's the case, I want to save that thing to a file, what's the right way to do this in a way that doesn't rely on me say, importing Plots.savefig
?
E.g. I'm imagining something like
function f(x)
if displayable("image/png", x)
open(tempname(), "w+") do io
print(io, "image/png", x) #this doesn't actually work as far as I can tell
end
else
# something else
end
end
Have you tried FileIO.jl?
https://juliaio.github.io/FileIO.jl/stable/
Wait a second, I just realized all I needed to do was use show
instead of print
:face_palm:
Correct solution is
function f(x)
if showable("image/png", x)
open(tempname(), "w+") do io
show(io, "image/png", x)
end
else
# something else
end
end
Mason Protter has marked this topic as resolved.
Chad Scherrer said:
Have you tried FileIO.jl?
https://juliaio.github.io/FileIO.jl/stable/
Thanks for the suggestion but I'm just doing absolute basics
FileIO has this example:
function save(f::File{format"PNG"}, data)
open(f, "w") do s
# Don't forget to write the magic bytes!
write(s, magic(format"PNG"))
# Do the rest of the stuff needed to save in PNG format
end
end
Yeah, that's quite nice I'll keep it in mind
Why do you use displayable
instead of showable
?
I actually meant to write showable
there, it was a typo in my MWE. Good eye!
Last updated: Nov 06 2024 at 04:40 UTC