forked from jkrumbiegel/JuliaCon2022Makie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_viewer.jl
94 lines (74 loc) · 1.9 KB
/
image_viewer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using JSON3
using HTTP
using FileIO
using GLMakie
using Dates
using Chain
using GLMakie.Colors
using Memoization
GLMakie.activate!()
GLMakie.set_window_config!(float=true)
##
metadata = open(JSON3.read, "images.json")
@memoize function load_image(url)
@chain begin
url
HTTP.get
_.body
IOBuffer
load
end
end
##
index = Observable(1)
entry = @lift metadata[$index]
img = lift(entry) do e
load_image(e["url"])
end
f = Figure(fontsize=30, figure_padding=40)
ax, im = image(f[1, 1], @lift($img'),
axis=(;
yreversed=true,
aspect=DataAspect(),
title=@lift($entry["title"])
)
)
hidedecorations!(ax)
ax2 = Axis(f[1, 2])
subset = Observable{Any}()
onany(img, ax.finallimits) do img, lims
(xlow, ylow), (xhigh, yhigh) = extrema(lims)
xlow = clamp(round(Int, xlow), 1, size(img, 2) + 1)
xhigh = clamp(round(Int, xhigh), 0, size(img, 2))
ylow = clamp(round(Int, ylow), 1, size(img, 1) + 1)
yhigh = clamp(round(Int, yhigh), 0, size(img, 1))
subset[] = @view img[ylow:yhigh, xlow:xhigh]
reset_limits!(ax2)
end
notify(img)
hist!(ax2, @lift(vec(red.($subset))), bins=range(0, 1, length=256), color=RGBAf(1, 0, 0, 0.3))
hist!(ax2, @lift(vec(green.($subset))), bins=range(0, 1, length=256), color=RGBAf(0, 1, 0, 0.3))
hist!(ax2, @lift(vec(blue.($subset))), bins=range(0, 1, length=256), color=RGBAf(0, 0, 1, 0.3))
ylims!(ax2, low=0)
xlims!(ax2, 0, 1)
gl = GridLayout(f[2, 1], tellwidth=false)
b = Button(gl[1, 2], label="Next")
function offset_index(b, i)
prevlabel = b.label[]
try
b.label = "Loading..."
index[] = mod1(index[] + i, length(metadata))
reset_limits!(ax)
reset_limits!(ax2)
finally
b.label = prevlabel
end
end
on(b.clicks) do c
@async offset_index(b, 1)
end
b2 = Button(gl[1, 1], label="Previous")
on(b2.clicks) do c
@async offset_index(b2, -1)
end
f