Simple image segmenter in Python

口-bw.1.png, 口-bw.2.png, 口-bw.3.png
So I was looking for a simple segmenter to break down images containing several tiles into single pieces. I decided to write one myself, so here it is.

segmenttiles.py comes with a help page (python segmenttiles.py --help) which explains the parameters in short. Most important segmentation can be done either by using a window or by looking for whitespaces in the image. Giving the width/height ratio or more specifically the tilesize makes guessing more accurate by discarding solutions that don't fit the given sizes. Furthermore by selecting equaltiles the segmenter will try to find a segmenting solution that results in having exact same size tiles. This can even improve segmentation results.

So, as an example you can download stroke order images from Wikimedia Commons (for this task Category:Bw.png_stroke_order_images is suited) and start the segmentation on more than 1000 files:

$ python segmenttiles.py --segmentation=whitespace --equaltiles --tilesize=120x110
--test bw.png/?-bw.png > bw.png.segmentations

These images have perfect white tile borders and most of them can be separated into equally sized tiles. The average tile size is 120 (width) x 110 (height). By specifying --test no actual work on the files is done, but the proposed segmentation positions are printed to stdout.

bw.png/干-bw.png        [-5, 116, 237, 358, 479]        [0, 115]
bw.png/平-bw.png        [-6, 115, 236, 357, 478, 599, 720]      [0, 126]
bw.png/对-bw.png        [-2, 119, 240, 361, 482, 603, 724, 845] [0, 117]
bw.png/容-bw.png        [-5, 116, 237, 358, 479, 600, 721, 842, 963, 1084] [-2, 120, 242]
bw.png/她-bw.png        [-1, 120, 241, 362, 483, 604, 725, 846] [0, 110]
bw.png/凹-bw.png        [14, 110, 206, 302, 398, 494, 590]      [0, 86]
[...]

Finally using this list the actual breaking down can be started:

$ python segmenttiles.py --readfrom=bw.png.segmentations -v bw.png/?-bw.png
Read 995 entries, ignored 5 lines
Processing bw.png/对-bw.png...
Grid 7x1, (-2,119,240,361,482,603,724,845), (0,117)
Writing tiles... 0 1 2 3 4 5 6 finished
Processing bw.png/容-bw.png...
Grid 9x2, (-5,116,237,358,479,600,721,842,963,1084), (-2,120,242)
Writing tiles... 0 1 2 3 4 5 6 7 8 9 10 empty empty empty empty empty empty empty finished
[...]

Overall 5 files can not be segmented. A bit more do not result in equal sizes and few of them might have a wrong segmentation. Playing around with the parameters helps.

AttachmentSize
segmenttiles.py.txt32.54 KB