Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageFilter
- from collections import deque
- class DSU_2D:
- d = [[]]
- p = [[]]
- def __init__(self, n, m):
- self.d = [[1 for _ in range(m)] for __ in range(n)]
- self.p = [[(i, j) for j in range(m)] for i in range(n)]
- def get(self, x, y):
- if self.p[x][y] == (x, y):
- return (x, y)
- self.p[x][y] = self.get(self.p[x][y][0], self.p[x][y][1])
- return self.p[x][y]
- def uni(self, a, b):
- a = self.get(a[0], a[1])
- b = self.get(b[0], b[1])
- if a == b:
- return False
- if self.d[a[0]][a[1]] < self.d[b[0]][b[1]]:
- a, b = b, a
- self.p[b[0]][b[1]] = a
- self.d[a[0]][a[1]] += self.d[b[0]][b[1]]
- return True
- white = (255, 255, 255)
- black = (0, 0, 0)
- def Filter(num):
- name = 'C:\\Users\\bossb\\Desktop\\for_Andrew\\ (' + str(num) + ').png'
- try:
- tmp = Image.open(name)
- except FileNotFoundError:
- return False
- def get_image(name):
- image = Image.open(name).convert('RGB')
- pixel_map = image.load()
- return image, pixel_map, image.size[0], image.size[1]
- image, pixel_map, w, h = get_image(name)
- def distance(a, b, coefs):
- ans = 0
- for i in range(3):
- ans += abs(a[i] - b[i]) * coefs[i];
- return ans
- def reverse():
- for x in range(w):
- for y in range(h):
- r, g, b = pixel_max[x, y]
- pixel_map[x, y] = 255 - r, 255 - g, 255 - b
- def generate_steps(dlt, flag):
- if flag:
- return [(i, j) for i in range(-dlt, dlt + 1) for j in range(-dlt, dlt + 1)]
- return [(i, j) for i in range(-dlt, dlt + 1) for j in range(-dlt, dlt + 1) if not (i == 0 and j == 0)]
- def connect(steps, border, coefs):
- dsu = DSU_2D(w, h)
- 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 distance(pixel_map[x, y], pixel_map[nx, ny], coefs) <= border:
- dsu.uni((x, y), (nx, ny))
- return dsu
- def drawing_rule(pixel_map, w, h):
- gray = (200, 200, 200)
- for x in range(w):
- up = 0
- down = 0
- for y in range(h):
- if pixel_map[x, y] == black:
- down += 1
- for y in range(h):
- if pixel_map[x, y] == black:
- up += 1
- down -= 1
- if up > 0 and down > 0:
- pixel_map[x, y] = gray
- for x in range(w):
- for y in range(h):
- if pixel_map[x, y] == gray:
- pixel_map[x, y] = black
- def find_start_point():
- for x in range(w):
- for y in range(h):
- if distance(white, pixel_map[x, y], [1.0, 1.0, 1.0]) >= 100:
- return (x, y)
- return (w - 1, h - 1)
- def kill_grad():
- q = deque()
- q.append(find_start_point())
- used = [[False for j in range(h)] for i in range(w)]
- used[0][0] = True
- steps = generate_steps(1, False)
- while len(q):
- x, y = q.popleft()
- for (dx, dy) in steps:
- nx = x + dx
- ny = y + dy
- if nx < 0 or ny < 0 or nx >= w or ny >= h or used[nx][ny]:
- continue
- if distance(pixel_map[x, y], pixel_map[nx, ny], [1.0, 1.0, 1.0]) <= 7:
- used[nx][ny] = True
- q.append((nx, ny))
- for x in range(w):
- for y in range(h):
- if used[x][y]:
- pixel_map[x, y] = white
- kill_grad()
- for x in range(w):
- for y in range(h):
- if pixel_map[x, y] != white:
- pixel_map[x, y] = black
- steps = generate_steps(1, True)
- dsu = connect(steps, 0, [1, 1, 1])
- mp = [[0 for j in range(h)] for i in range(w)]
- for x in range(w):
- for y in range(h):
- if dsu.get(x, y) != (x, y):
- continue
- color = int(255.0 * dsu.d[x][y] / w / h)
- if color > 100:
- color = white
- else:
- color = black
- if dsu.d[x][y] <= 3:
- color = white
- mp[x][y] = color
- for x in range(w):
- for y in range(h):
- a, b = dsu.get(x, y)
- pixel_map[x, y] = mp[a][b]
- image2, pixel_map2, w, h = get_image(name)
- drawing_rule(pixel_map, w, h)
- for x in range(w):
- for y in range(h):
- if pixel_map[x, y] == white:
- pixel_map2[x, y] = white
- steps = generate_steps(1, False)
- mp = {}
- 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 pixel_map2[nx, ny] == white and pixel_map2[x, y] != white:
- good = True
- break
- if good:
- if pixel_map2[x, y] not in mp:
- mp[pixel_map2[x, y]] = 0
- mp[pixel_map2[x, y]] += 1
- mx = 0
- clr = white
- border = 10
- coefs = [1.0, 1.0, 1.0]
- for val in mp:
- cur = 0
- for val2 in mp:
- if distance(val, val2, coefs) <= border:
- cur += mp[val2]
- if mx < cur:
- mx = cur
- clr = val
- for x in range(w):
- for y in range(h):
- if distance(pixel_map2[x, y], clr, [1.0, 1.0, 1.0]) <= 50:
- pixel_map2[x, y] = black
- def Func():
- mp = {}
- for x in range(w):
- for y in range(h):
- c = pixel_map2[x, y]
- if c == black or c == white:
- continue
- if c not in mp:
- mp[c] = 0
- mp[c] += 1
- clr = white
- mx = 0
- for val in mp:
- if mp[val] > mx:
- mx = mp[val]
- clr = val
- for x in range(w):
- for y in range(h):
- if distance(pixel_map2[x, y], clr, [1.0, 1.0, 1.0]) <= 100:
- pixel_map2[x, y] = white
- Func()
- for x in range(w):
- for y in range(h):
- if pixel_map2[x, y] != black:
- pixel_map2[x, y] = white
- image2.show()
- return True
- for num in range(50):
- Filter(num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement