Skip to content

Commit 5f91dfb

Browse files
committed
Support globs and multiple sheets
1 parent 3deec57 commit 5f91dfb

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "1.0.0"
55

66
[deps]
77
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
8+
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
89
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
910
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
1011
ImageMorphology = "787d08f9-d448-5407-9aad-5290dd7ab264"
@@ -20,6 +21,7 @@ CounterMarkingImageViewExt = "ImageView"
2021

2122
[compat]
2223
FileIO = "1"
24+
Glob = "1"
2325
ImageCore = "0.10"
2426
ImageIO = "0.6"
2527
ImageMorphology = "0.4"

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ julia> writexlsx(raw"C:\Users\me\somefolder\mydata.xlsx", seg)
162162
If you have many images in one folder, you can process them all using a single command:
163163

164164
```
165-
julia> process_images("2025-03-15/*.png")
165+
julia> process_images("2025-03-15/results.xlsx", "2025-03-15/*.png")
166166
```
167167

168168
### Step 8: create a "density map" of marks across multiple images

src/CounterMarking.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ using ImageSegmentation
55
using ImageMorphology: label_components
66
using FileIO
77
using XLSX
8+
using Glob
89

910
export segment_image, stimulus_index, spots, Spot, upperleft
10-
export writexlsx
11+
export writexlsx, process_images
1112
export randshow, meanshow
1213

1314
include("segment.jl")

src/xlxs.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,39 @@ function writexlsx(filename::AbstractString, seg::SegmentedImage)
3636
spotdict, stimulus = spots(seg)
3737
writexlsx(filename, spotdict, stimulus, imgsize)
3838
end
39+
40+
"""
41+
process_images(outfile::AbstractString, glob::GlobMatch; dirname=pwd())
42+
process_images(outfile::AbstractString, glob::AbstractString; dirname=pwd())
43+
44+
Process all images with filenames matching `glob` and save the results to `outfile`.
45+
Each image will be a separate sheet in the Excel file.
46+
47+
Optionally specify the `dirname` containing the images.
48+
49+
# Examples
50+
51+
To process a collection of images in a different directory, and save the results to
52+
that same directory:
53+
54+
```julia
55+
julia> process_images("2025-03-15/results.xlsx", glob"*.png"; dirname="2025-03-15")
56+
```
57+
"""
58+
function process_images(outfile::AbstractString, glob::Glob.GlobMatch; dirname=pwd())
59+
i = 0
60+
XLSX.openxlsx(outfile; mode="w") do xf
61+
for filename in readdir(glob, dirname)
62+
img = load(filename)
63+
seg = segment_image(img)
64+
imgsize = size(labels_map(seg))
65+
spotdict, stimulus = spots(seg)
66+
sheetname = splitext(basename(filename))[1]
67+
sheet = xf[i+=1]
68+
XLSX.rename!(sheet, sheetname)
69+
makesheet!(sheet, spotdict, stimulus, imgsize)
70+
end
71+
end
72+
end
73+
process_images(outfile::AbstractString, glob::AbstractString; kwargs...) =
74+
process_images(outfile, Glob.GlobMatch(glob); kwargs...)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CounterMarking
22
using FileIO
3+
using XLSX
34
using Test
45

56
@testset "CounterMarking.jl" begin
@@ -38,4 +39,9 @@ using Test
3839
tmpfile = tempname() * ".xlsx"
3940
writexlsx(tmpfile, seg)
4041
@test isfile(tmpfile)
42+
43+
# Test multi-file writing
44+
process_images(tmpfile, glob"*.png"; dirname=testdir)
45+
data = XLSX.readtable(tmpfile, "Picture")
46+
@test isa(data, XLSX.DataTable)
4147
end

0 commit comments

Comments
 (0)