-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha_treatment.py
32 lines (26 loc) · 1.02 KB
/
captcha_treatment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import glob
import os
import cv2
from PIL import Image
def treat_images(origin_folder, destiny_folder='images/treated_images'):
files = glob.glob(f"{origin_folder}/*")
for file in files:
image = cv2.imread(file)
grey_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
_, treated_image = cv2.threshold(
grey_image, 127, 255, cv2.THRESH_TRUNC or cv2.THRESH_OTSU)
filename = os.path.basename(file)
cv2.imwrite(
f"{destiny_folder}/{filename}", treated_image)
files = glob.glob(f"{destiny_folder}/*")
for file in files:
image = Image.open(file)
image = image.convert("P")
copy_image = Image.new("P", image.size, 255)
for x in range(image.size[1]):
for y in range(image.size[0]):
pixel_color = image.getpixel((y, x))
if(pixel_color < 115):
copy_image.putpixel((y, x), 0)
filename = os.path.basename(file)
copy_image.save(f'{destiny_folder}/{filename}')