Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageFilter
- try:
- im = Image.open("C:\\Users\\bossb\\Desktop\\21.png")
- except FileNotFoundError:
- print("Файл не найден")
- im = im.convert('RGB')
- w, h = im.size
- img = im
- im = im.load()
- p = [[(i, j) for j in range(h)] for i in range(w)]
- d = [[1 for j in range(h)] for i in range(w)]
- def get(x, y):
- if p[x][y] == (x, y):
- return (x, y)
- p[x][y] = get(p[x][y][0], p[x][y][1])
- return p[x][y]
- def uni(a, b):
- a = get(a[0], a[1])
- b = get(b[0], b[1])
- if a == b:
- return
- if d[a[0]][a[1]] < d[b[0]][b[1]]:
- a, b = b, a
- p[b[0]][b[1]] = a
- d[a[0]][a[1]] += d[b[0]][b[1]]
- def areConnected(a, b):
- return abs(a[0] - b[0]) + abs(a[1] - b[1]) + abs(a[2] - b[2]) < 15
- for x in range(w):
- for y in range(h):
- for dx in range(2):
- for dy in range(2):
- if dx == 0 and dy == 0:
- continue
- nx = x + dx
- ny = y + dy
- if nx < 0 or ny < 0 or nx >= w or ny >= h:
- continue
- if areConnected(im[x, y], im[nx, ny]):
- uni((x, y), (nx, ny))
- mp = {}
- for x in range(w):
- for y in range(h):
- par = get(x, y)
- if par not in mp:
- mp[par] = 0
- mp[par] += 1
- sz = w * h
- for value in mp:
- cnt = mp[value]
- x = int(255.0 * cnt / sz)
- if x > 100:
- x = 255
- else:
- x = 0
- if cnt < 3:
- x = 255
- mp[value] = (x, x, x)
- for x in range(w):
- for y in range(h):
- im[x, y] = mp[get(x, y)]
- img.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement