/
TestImages.jl
118 lines (99 loc) · 3.25 KB
/
TestImages.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module TestImages
using FileIO, AxisArrays
using Pkg.Artifacts
const artifacts_toml = abspath(joinpath(@__DIR__, "..", "Artifacts.toml"))
export testimage
REPO_URL = "https://github.com/JuliaImages/TestImages.jl/blob/gh-pages/images/"
remotefiles = [
"autumn_leaves.png" ,
"blobs.gif" ,
"cameraman.tif" ,
"earth_apollo17.jpg" ,
"fabio_color_256.png" ,
"fabio_color_512.png" ,
"fabio_gray_256.png" ,
"fabio_gray_512.png" ,
"hela-cells.tif" ,
"house.tif" ,
"jetplane.tif" ,
"lake_color.tif" ,
"lake_gray.tif" ,
"lena_color_256.tif" ,
"lena_color_512.tif" ,
"lena_gray_256.tif" ,
"lena_gray_512.tif" ,
"lena_gray_16bit.png" ,
"livingroom.tif" ,
"lighthouse.png" ,
"mandril_color.tif" ,
"mandril_gray.tif" ,
"mandrill.tiff" ,
"m51.tif" ,
"moonsurface.tiff" ,
"mountainstream.png" ,
"mri-stack.tif" ,
"multi-channel-time-series.ome.tif" ,
"peppers_color.tif" ,
"peppers_gray.tif" ,
"pirate.tif" ,
"toucan.png" ,
"walkbridge.tif" ,
"woman_blonde.tif" ,
"woman_darkhair.tif" ,
]
"""
img = testimage(filename; download_only=false, [ops...])
Load test image that partially matches `filename`, the first match is used if there're more
than one.
If `download_only=true`, the full filepath is returned.
Any other keyword arguments `ops` will be passed to image IO backend through `FileIO.load`.
# Example
```julia
julia> using TestImages
julia> img = testimage("cameraman.tif"); # fullname
julia> img = testimage("cameraman"); # without extension works
julia> img = testimage("c"); # with only partial name also works
```
# Extended help
The following is a complete list of testimages, you can also check them at
https://testimages.juliaimages.org/
$(reduce((x, y)->join([x, "\n - \`\"", splitext(y)[1], "\"\`"]), sort(remotefiles); init=""))
"""
function testimage(filename; download_only = false, ops...)
imagefile = image_path(full_imagename(filename))
download_only && return imagefile
img = load(imagefile; ops...)
if basename(imagefile) == "mri-stack.tif"
# orientation is posterior-right-superior,
# see http://www.grahamwideman.com/gw/brain/orientation/orientterms.htm
return AxisArray(img, (:P, :R, :S), (1, 1, 5))
end
img
end
"""
fullname = full_imagename(shortname)
Get the first match of `shortname` in `TestImages.remotefiles`
"""
function full_imagename(filename)
idx = findfirst(remotefiles) do x
startswith(x, filename)
end
idx === nothing && throw(ArgumentError("$filename not found in the online repository, use `TestImages.remotefiles` to get a full list of test images."))
return remotefiles[idx]
end
function image_path(imagename)
file_hash = artifact_hash(imagename, artifacts_toml)
if file_hash === nothing || !artifact_exists(file_hash)
new_hash = create_artifact() do artifact_dir
download(REPO_URL*imagename*"?raw=true", joinpath(artifact_dir, imagename))
end
if file_hash === nothing
bind_artifact!(artifacts_toml,
imagename,
new_hash)
end
file_hash = new_hash
end
return joinpath(artifact_path(file_hash), imagename)
end
end # module