Advertisement
999ms

Untitled

Mar 31st, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. from PIL import Image, ImageFilter
  2. try:  
  3.     im = Image.open("C:\\Users\\bossb\\Desktop\\captcha_7.bin")  
  4. except FileNotFoundError:  
  5.     print("Файл не найден")
  6. im = im.convert('RGB')
  7. colors = [0 for i in range(256)]
  8.  
  9.  
  10. w, h = im.size
  11. img = im
  12. im = im.load()
  13. img.show()
  14.  
  15. arr = [0, 255]
  16. def FIX(border_color):
  17.     for i in range(w):
  18.         for j in range(h):
  19.             r, g, b = im[i, j]
  20.             good = 0
  21.             for clr in arr:
  22.                 if abs(good - r) > abs(clr - r):
  23.                     good = clr
  24.             if i == 0 or j == 0 or i == w - 1 or j == h - 1:
  25.                 good = border_color
  26.             im[i, j] = (good, good, good)
  27.  
  28.     vb = [0 for _ in range(w)]
  29.     hb = [0 for _ in range(h)]
  30.  
  31.     for i in range(w - 1):
  32.         for j in range(h):
  33.             if im[i, j] != im[i + 1, j]:
  34.                 vb[i] += 1
  35.  
  36.     for i in range(w):
  37.         pre = 0
  38.         for j in range(h - 1):
  39.             if im[i, j] != im[i, j + 1]:
  40.                 hb[j] += 1
  41.  
  42.     drLines = False
  43.     drRect = True  
  44.  
  45.     def WriteLine(x, y, dx, dy, clr):
  46.         if not drLines:
  47.             return
  48.         for i in range(dx):
  49.             for j in range(dy):
  50.                 im[x + i, y + j] = clr
  51.  
  52.     x_lines = [0]
  53.     y_lines = [0]
  54.  
  55.     V_PAR = 20
  56.     H_PAR = 40
  57.  
  58.     for i in range(w):
  59.         if vb[i] > V_PAR:
  60.             WriteLine(i, 0, 1, h, (0, 255, 0))
  61.             x_lines.append(i)        
  62.     x_lines.append(w - 1)
  63.  
  64.     black = (0, 0, 0)
  65.     white = (255, 255, 255)
  66.  
  67.     for i in range(h):
  68.         if hb[i] > H_PAR:
  69.             WriteLine(0, i, w, 1, (255, 0, 0))
  70.             y_lines.append(i)
  71.  
  72.     y_lines.append(h - 1)
  73.  
  74.     def ReverseRectangle(x, y, xto, yto):
  75.         if not drRect:
  76.             return
  77.         for i in range(xto - x):
  78.             for j in range(y_to - y):
  79.                 if im[x + i, y + j] == white:
  80.                     im[x + i, y + j] = black
  81.                 else:
  82.                     im[x + i, y + j] = white
  83.  
  84.     def Fix1():
  85.         for i in range(len(y_lines) - 1):
  86.             y_from = y_lines[i]
  87.             y_to = y_lines[i + 1]
  88.             tot = y_to - y_from + 1
  89.             if tot == 0:
  90.                 continue
  91.             for x in x_lines:
  92.                 if x >= w - 1:
  93.                     continue
  94.                 cur = 0
  95.                 for j in range(tot):
  96.                     if im[x, y_from + j] != im[x + 1, y_from + j]:
  97.                         cur += 1
  98.                 if cur / tot > 0.35:
  99.                     ReverseRectangle(x + 1, y_from + 1, w, y_to)
  100.  
  101.     def Reverse():
  102.         for j in range(h):
  103.             for i in range(w // 2):
  104.                 im[i, j], im[w - 1 -i, j] = im[w - 1 - i, j], im[i, j]
  105.  
  106.         for i in range(len(x_lines)):
  107.             x_lines[i] = w - x_lines[i] - 1
  108.         m = len(x_lines)
  109.         for i in range(m // 2):
  110.             x_lines[i], x_lines[m - 1 - i] = x_lines[m - 1 - i], x_lines[i]
  111.  
  112.     Fix1()
  113.     Reverse()
  114.     Fix1()
  115.     Reverse()
  116.     img.show()
  117.                        
  118. FIX(255)
  119. FIX(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement