def joined_fragments image assert image mode is frags for in xy image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def joined_fragments(image):
assert image.mode is '1'
frags = {}
for x, y in xy(image):
if image.getpixel((x,y)) == 0: # black pixel
new_fragment = set([(x, y)])
frags[(x,y)] = new_fragment
for dot in (
(x-1, y-1), (x+0, y-1), (x+1, y-1),
(x-1, y+0),):# (x+1, y+0),
if frags.has_key(dot):
new_fragment.update(frags[dot])
for key in frags[dot]:
frags[key] = new_fragment
return set(imap(tuple, frags.values()))