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\\captcha_7.bin")
- except FileNotFoundError:
- print("Файл не найден")
- im = im.convert('RGB')
- colors = [0 for i in range(256)]
- w, h = im.size
- img = im
- im = im.load()
- img.show()
- arr = [0, 255]
- def FIX(border_color):
- for i in range(w):
- for j in range(h):
- r, g, b = im[i, j]
- good = 0
- for clr in arr:
- if abs(good - r) > abs(clr - r):
- good = clr
- if i == 0 or j == 0 or i == w - 1 or j == h - 1:
- good = border_color
- im[i, j] = (good, good, good)
- vb = [0 for _ in range(w)]
- hb = [0 for _ in range(h)]
- for i in range(w - 1):
- for j in range(h):
- if im[i, j] != im[i + 1, j]:
- vb[i] += 1
- for i in range(w):
- pre = 0
- for j in range(h - 1):
- if im[i, j] != im[i, j + 1]:
- hb[j] += 1
- drLines = False
- drRect = True
- def WriteLine(x, y, dx, dy, clr):
- if not drLines:
- return
- for i in range(dx):
- for j in range(dy):
- im[x + i, y + j] = clr
- x_lines = [0]
- y_lines = [0]
- V_PAR = 20
- H_PAR = 40
- for i in range(w):
- if vb[i] > V_PAR:
- WriteLine(i, 0, 1, h, (0, 255, 0))
- x_lines.append(i)
- x_lines.append(w - 1)
- black = (0, 0, 0)
- white = (255, 255, 255)
- for i in range(h):
- if hb[i] > H_PAR:
- WriteLine(0, i, w, 1, (255, 0, 0))
- y_lines.append(i)
- y_lines.append(h - 1)
- def ReverseRectangle(x, y, xto, yto):
- if not drRect:
- return
- for i in range(xto - x):
- for j in range(y_to - y):
- if im[x + i, y + j] == white:
- im[x + i, y + j] = black
- else:
- im[x + i, y + j] = white
- def Fix1():
- for i in range(len(y_lines) - 1):
- y_from = y_lines[i]
- y_to = y_lines[i + 1]
- tot = y_to - y_from + 1
- if tot == 0:
- continue
- for x in x_lines:
- if x >= w - 1:
- continue
- cur = 0
- for j in range(tot):
- if im[x, y_from + j] != im[x + 1, y_from + j]:
- cur += 1
- if cur / tot > 0.35:
- ReverseRectangle(x + 1, y_from + 1, w, y_to)
- def Reverse():
- for j in range(h):
- for i in range(w // 2):
- im[i, j], im[w - 1 -i, j] = im[w - 1 - i, j], im[i, j]
- for i in range(len(x_lines)):
- x_lines[i] = w - x_lines[i] - 1
- m = len(x_lines)
- for i in range(m // 2):
- x_lines[i], x_lines[m - 1 - i] = x_lines[m - 1 - i], x_lines[i]
- Fix1()
- Reverse()
- Fix1()
- Reverse()
- img.show()
- FIX(255)
- FIX(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement