Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageFilter
- def F(num):
- name = "C:\\Users\\bossb\\Desktop\\" + str(num) + ".jpeg"
- try:
- im = Image.open(name)
- except FileNotFoundError:
- return
- 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]) * 1.125 + abs(a[1] - b[1]) * 0.15 + abs(a[2] - b[2]) * 3 <= 50
- steps = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if not (i == 0 and j == 0)]
- for x in range(w):
- for y in range(h):
- for (dx, dy) in steps:
- 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 < 2:
- x = 255
- mp[value] = (x, x, x)
- for x in range(w):
- for y in range(h):
- im[x, y] = mp[get(x, y)]
- if True:
- for x in range(w):
- for y in range(h):
- p[x][y] = (x, y)
- d[x][y] = 1
- steps = [(i, j) for i in range(-2, 3) for j in range(-2, 3)]
- mp = {}
- for x in range(w):
- for y in range(h):
- for (dx, dy) in steps:
- if dx + dy == 0:
- continue
- nx = x + dx
- ny = y + dy
- if nx >= w or ny >= h:
- continue
- if im[x, y] == im[nx, ny]:
- uni((x, y), (nx, ny))
- for x in range(w):
- for y in range(h):
- px, py = get(x, y)
- if d[px][py] <= 15:
- im[px, py] = (255, 255, 255)
- mp[(px, py)] = im[px, py]
- for x in range(w):
- for y in range(h):
- im[x, y] = mp[get(x, y)]
- im2 = Image.open(name).convert('RGB')
- w, h = im2.size
- img2 = im2
- im2 = im2.load()
- for x in range(w):
- for y in range(h):
- if im[x, y] != (0, 0, 0):
- im2[x, y] = (255, 255, 255)
- for x in range(w):
- for y in range(h):
- d[x][y] = 1
- p[x][y] = (x, y)
- mp = {}
- steps = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if not (i == 0 and j == 0)]
- white = (255, 255, 255)
- for x in range(w):
- for y in range(h):
- good = False
- for (dx, dy) in steps:
- nx = x + dx
- ny = y + dy
- if nx < 0 or ny < 0 or nx >= w or ny >= h:
- continue
- if im2[nx, ny] == white and im[x, y] != white:
- good = True
- break
- if good:
- if im2[x, y] not in mp:
- mp[im2[x, y]] = 0
- mp[im2[x, y]] += 1
- mx = 0
- clr = white
- mp2 = {}
- def dist(a, b):
- return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2
- for val in mp:
- cur = 0
- for val2 in mp:
- if dist(val, val2) <= 25:
- cur += mp[val2]
- if mx < cur:
- mx = cur
- clr = val
- for x in range(w):
- for y in range(h):
- if im2[x, y] == clr:
- im2[x, y] = (0, 0, 0)
- img2.show()
- for num in range(50):
- F(num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement