Merge pull request #597 from tbreloff/dev
Output/Animation improvements
This commit is contained in:
commit
d8ffd729f4
@ -57,6 +57,7 @@ export
|
|||||||
savefig,
|
savefig,
|
||||||
png,
|
png,
|
||||||
gui,
|
gui,
|
||||||
|
inline,
|
||||||
closeall,
|
closeall,
|
||||||
|
|
||||||
backend,
|
backend,
|
||||||
|
|||||||
@ -53,21 +53,29 @@ immutable AnimatedGif
|
|||||||
filename::String
|
filename::String
|
||||||
end
|
end
|
||||||
|
|
||||||
|
file_extension(fn) = Base.Filesystem.splitext(fn)[2][2:end]
|
||||||
|
|
||||||
gif(anim::Animation, fn = giffn(); kw...) = buildanimation(anim.dir, fn; kw...)
|
gif(anim::Animation, fn = giffn(); kw...) = buildanimation(anim.dir, fn; kw...)
|
||||||
mov(anim::Animation, fn = movfn(); kw...) = buildanimation(anim.dir, fn; kw...)
|
mov(anim::Animation, fn = movfn(); kw...) = buildanimation(anim.dir, fn; kw...)
|
||||||
mp4(anim::Animation, fn = mp4fn(); kw...) = buildanimation(anim.dir, fn; kw...)
|
mp4(anim::Animation, fn = mp4fn(); kw...) = buildanimation(anim.dir, fn; kw...)
|
||||||
|
|
||||||
|
const _imagemagick_initialized = Ref(false)
|
||||||
|
|
||||||
function buildanimation(animdir::AbstractString, fn::AbstractString;
|
function buildanimation(animdir::AbstractString, fn::AbstractString;
|
||||||
fps::Integer = 20, loop::Integer = 0)
|
fps::Integer = 20, loop::Integer = 0)
|
||||||
fn = abspath(fn)
|
fn = abspath(fn)
|
||||||
try
|
try
|
||||||
# high quality
|
if !_imagemagick_initialized[]
|
||||||
speed = round(Int, 100 / fps)
|
|
||||||
file = joinpath(Pkg.dir("ImageMagick"), "deps","deps.jl")
|
file = joinpath(Pkg.dir("ImageMagick"), "deps","deps.jl")
|
||||||
if isfile(file) && !haskey(ENV, "MAGICK_CONFIGURE_PATH")
|
if isfile(file) && !haskey(ENV, "MAGICK_CONFIGURE_PATH")
|
||||||
include(file)
|
include(file)
|
||||||
end
|
end
|
||||||
|
_imagemagick_initialized[] = true
|
||||||
|
end
|
||||||
|
|
||||||
# prefix = get(ENV, "MAGICK_CONFIGURE_PATH", "")
|
# prefix = get(ENV, "MAGICK_CONFIGURE_PATH", "")
|
||||||
|
# high quality
|
||||||
|
speed = round(Int, 100 / fps)
|
||||||
run(`convert -delay $speed -loop $loop $(joinpath(animdir, "*.png")) -alpha off $fn`)
|
run(`convert -delay $speed -loop $loop $(joinpath(animdir, "*.png")) -alpha off $fn`)
|
||||||
|
|
||||||
catch err
|
catch err
|
||||||
@ -88,7 +96,14 @@ end
|
|||||||
|
|
||||||
# write out html to view the gif... note the rand call which is a hack so the image doesn't get cached
|
# write out html to view the gif... note the rand call which is a hack so the image doesn't get cached
|
||||||
function Base.show(io::IO, ::MIME"text/html", agif::AnimatedGif)
|
function Base.show(io::IO, ::MIME"text/html", agif::AnimatedGif)
|
||||||
write(io, "<img src=\"$(relpath(agif.filename))?$(rand())>\" />")
|
ext = file_extension(agif.filename)
|
||||||
|
write(io, if ext == "gif"
|
||||||
|
"<img src=\"$(relpath(agif.filename))?$(rand())>\" />"
|
||||||
|
elseif ext in ("mov", "mp4")
|
||||||
|
"<video controls><source src=\"$(relpath(agif.filename))?$(rand())>\" type=\"video/$ext\"></video>"
|
||||||
|
else
|
||||||
|
error("Cannot show animation with extension $ext: $agif")
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -122,6 +122,13 @@ savefig(fn::AbstractString) = savefig(current(), fn)
|
|||||||
|
|
||||||
gui(plt::Plot = current()) = display(PlotsDisplay(), plt)
|
gui(plt::Plot = current()) = display(PlotsDisplay(), plt)
|
||||||
|
|
||||||
|
# IJulia only... inline display
|
||||||
|
function inline(plt::Plot = current())
|
||||||
|
isijulia() || error("inline() is IJulia-only")
|
||||||
|
Main.IJulia.clear_output(true)
|
||||||
|
display(Main.IJulia.InlineDisplay(), plt)
|
||||||
|
end
|
||||||
|
|
||||||
function Base.display(::PlotsDisplay, plt::Plot)
|
function Base.display(::PlotsDisplay, plt::Plot)
|
||||||
prepare_output(plt)
|
prepare_output(plt)
|
||||||
_display(plt)
|
_display(plt)
|
||||||
@ -130,6 +137,13 @@ end
|
|||||||
# override the REPL display to open a gui window
|
# override the REPL display to open a gui window
|
||||||
Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::Plot) = gui(plt)
|
Base.display(::Base.REPL.REPLDisplay, ::MIME"text/plain", plt::Plot) = gui(plt)
|
||||||
|
|
||||||
|
|
||||||
|
_do_plot_show(plt, showval::Bool) = showval && gui(plt)
|
||||||
|
function _do_plot_show(plt, showval::Symbol)
|
||||||
|
showval == :gui && gui(plt)
|
||||||
|
showval in (:inline,:ijulia) && inline(plt)
|
||||||
|
end
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
|
|
||||||
const _mimeformats = Dict(
|
const _mimeformats = Dict(
|
||||||
|
|||||||
11
src/plot.jl
11
src/plot.jl
@ -126,9 +126,7 @@ function plot(plt1::Plot, plts_tail::Plot...; kw...)
|
|||||||
|
|
||||||
# finish up
|
# finish up
|
||||||
current(plt)
|
current(plt)
|
||||||
if get(d, :show, default(:show))
|
_do_plot_show(plt, get(d, :show, default(:show)))
|
||||||
gui()
|
|
||||||
end
|
|
||||||
plt
|
plt
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -234,9 +232,10 @@ function _plot!(plt::Plot, d::KW, args::Tuple)
|
|||||||
current(plt)
|
current(plt)
|
||||||
|
|
||||||
# do we want to force display?
|
# do we want to force display?
|
||||||
if plt[:show]
|
# if plt[:show]
|
||||||
gui(plt)
|
# gui(plt)
|
||||||
end
|
# end
|
||||||
|
_do_plot_show(plt, plt[:show])
|
||||||
|
|
||||||
plt
|
plt
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user