# Demo entry 6730832

233

Submitted by anonymous on Apr 09, 2018 at 09:17
Language: Python 3. Code size: 2.5 kB.

```from PIL import Image
def __init__(self, img):
self.width, self.height = img.size
self.offsets = [(0, 1), (0, -1), (1, 0), (-1, 0)]
self.pixels = list(img.getdata())
self.pixels = [self.pixels[i * self.width:(i + 1) * self.width] for i in range(self.height)]
self.visit = [[False for y in range(self.width)] for x in range(self.height)]
self.cluster = []
self.pixels_with_no_background = [[(0, 0, 0) for y in range(self.width)] for x in range(self.height)]

def dfs(self, x, y):
self.minx, self.maxx, self.miny, self.maxy = min(self.minx, x), max(self.maxx, x), min(self.miny, y), max(self.maxy, y)
self.visit[x][y] = True
cnt = 1
self.cluster.append((x, y))
for xoffset, yoffset in self.offsets:
xx, yy = x + xoffset, y + yoffset
if xx >= 0 and xx < self.height and yy >= 0 and yy < self.width:
if self.pixels[xx][yy] == self.pixels[x][y] and self.visit[xx][yy] == False:
cnt += self.dfs(xx, yy)
return cnt

def solve(self):
char_img_list = []
for y in range(self.width):
for x in range(self.height):
if self.visit[x][y] == False:
self.minx, self.maxx, self.miny, self.maxy = self.height, 0, self.width, 0
self.cluster = []
cnt = self.dfs(x, y)
if cnt >= 60:
for x, y in self.cluster:
self.pixels_with_no_background[x][y] = self.pixels[x][y]
character = [tuple(pixel) for raw in self.pixels_with_no_background[self.minx:self.maxx + 1] for pixel in
raw[self.miny:self.maxy + 1]]
char_img = Image.new('RGB', (self.maxy - self.miny + 1, self.maxx - self.minx + 1))
char_img.putdata(character)
char_img_list.append(char_img)
# char_img.save(str(char_img_num), 'PNG')
return char_img_list
pixels_with_no_background = [tuple(pixel) for raw in pixels_with_no_background for pixel in raw]
img_with_no_background = Image.new('RGB', (width, height))
img_with_no_background.putdata(pixels_with_no_background)
# img_with_no_background.show()
img_binarization = binarization(img_with_no_background)
# img_binarization.show()
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.