代码拉取完成,页面将自动刷新
#!/usr/bin/python3
# _*_ coding:utf-8 -*-
# __author__ = '鲁江洋'
# __time__ = '2020/5/25 10:10'
from package_all import *
template=__import__('template',fromlist=('template'))
from orc_scheduling import ORC
global set_new
set_new={}
# TODO:图像处理模块
class time_num(object,metaclass=getattr(template,"MyMetaClass")):
'''
作为装饰器使用,返回函数执行需要花费的时间
'''
def time_this_function(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print("程序运行时间",func.__name__, end - start)
return result
return wrapper
class Cope_image(object, metaclass=getattr(template,"MyMetaClass")):
'''
对验证码进行锐度增强,对比度增强,亮度增强,灰度处理,二值化,去干扰线算法,降噪算法
'''
def __init__(self, path:str,i):
'''
初始化
:param path:
'''
self.path=path
self.num=i
print(self.path)
def __call__(self, *args, **kwargs):
'''
:param args:
:param kwargs:
:return:
'''
self.image_Initial(self.__num)
@classmethod
@time_num.time_this_function
def image_orc(cls,image_path,num,statu=False):
if statu:
set_new['{}'.format(num)]=set()
result=ORC.dispatch_orc(image_path)
print("结果",result)
if len(result)!=0:
if isinstance(result, list):
for i in result:
print("进入")
i=re.findall('[\u4e00-\u9fa5]',i)
if len(i)==0:
continue
else:
for j in result:
if num not in set_new.keys():
set_new['{}'.format(num)]={j}
else:
set_new['{}'.format(num)].add(j)
print("成功添加")
def image_Initial(self,num):
'''
初始图像
:param path:
:return:
'''
if not os.path.exists('./deal_with_image'):
os.mkdir('./deal_with_image')
images = Image.open('{}'.format(self.__path))
images.save('./deal_with_image/image_Initial{}.png'.format(num))
self.image_orc('./deal_with_image/image_Initial{}.png'.format(num),num,True)
self.enhance_Contrast(images,num)
@classmethod
@time_num.time_this_function
def enhance_Contrast(cls, images:str,num,contrast:float=1.5):
'''
增强亮度
:param num:
:param contrast:
:param images:
:return:
'''
enh_con = ImageEnhance.Contrast(images)
image_contrasted = enh_con.enhance(contrast)
image_contrasted.save('./deal_with_image/image_contrasted{}.png'.format(num))
cls.image_orc('./deal_with_image/image_contrasted{}.png'.format(num),num)
cls.enhance_Brightness(image_contrasted,num)
# @time_num.timethis
@classmethod
@time_num.time_this_function
def enhance_Brightness(cls, image_contrasted:str,num,brightness:float=1.5):
'''
增强色彩
:param num:
:param brightness:
:param image_contrasted:
:param images:
:return:
'''
enh_bri = ImageEnhance.Brightness(image_contrasted)
image_brightened = enh_bri.enhance(brightness)
image_brightened.save('./deal_with_image/image_brightened{}.png'.format(num))
cls.image_orc('./deal_with_image/image_brightened{}.png'.format(num),num)
cls.enhance_Color(image_brightened,num)
@classmethod
@time_num.time_this_function
def enhance_Color(cls, image_brightened:str, num,color:float=1.5):
'''
增强色度
:param image_brightened:
:param num:
:param color:
:return:
'''
enh_col = ImageEnhance.Color(image_brightened)
image_colored = enh_col.enhance(color)
image_colored.save('./deal_with_image/image_colored{}.png'.format(num))
cls.image_orc('./deal_with_image/image_colored{}.png'.format(num),num)
cls.enhance_Sharpness(image_colored,num)
@classmethod
@time_num.time_this_function
def enhance_Sharpness(cls, image_colored, num,sharpness:float=3.0):
'''
增强锐度
:param image_colored:
:param num:
:param sharpness:
:return:
'''
enh_sha = ImageEnhance.Sharpness(image_colored)
image_sharped = enh_sha.enhance(sharpness)
image_sharped.save('./deal_with_image/image_sharped{}.png'.format(num))
cls.image_orc('./deal_with_image/image_sharped{}.png'.format(num),num)
cls.Image_gray(image_sharped,num)
@classmethod
@time_num.time_this_function
def Image_gray(cls, image_sharped,num, kind="L"):
'''
灰度处理
:param image_sharped:
:param num:
:param kind:
:return:
'''
# 进行灰度处理
image = image_sharped.convert(kind)
image.save('./deal_with_image/image_gray{}.png'.format(num))
cls.image_orc('./deal_with_image/image_gray{}.png'.format(num),num)
cls.Image_Binary_value(image,num)
@classmethod
@time_num.time_this_function
def Image_Binary_value(cls, image:str, num,threshold:float=49.0):
'''
二值化
:param image:
:param num:
:param threshold:
:return:
'''
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
image = image.point(table, '1')
image.save('./deal_with_image/image_binary_value{}.png'.format(num))
cls.image_orc('./deal_with_image/image_binary_value{}.png'.format(num),num)
cls.Image_away_line(image,num)
@classmethod
@time_num.time_this_function
def Image_away_line(cls, image:str,num):
'''
去干扰线算法
:param image:
:param num:
:return:
'''
img_array = image.load()
width = image.size[0]
height = image.size[1]
print("正在进行去干扰线算法")
for i in range(0, 1000):
for x in range(1, width - 1):
for y in range(1, height - 1):
count = 0
if img_array[x, y] == img_array[x - 1, y + 1]:
count += 1
if img_array[x, y] == img_array[x, y + 1]:
count += 1
if img_array[x, y] == img_array[x + 1, y + 1]:
count += 1
if img_array[x, y] == img_array[x - 1, y]:
count += 1
if img_array[x, y] == img_array[x + 1, y]:
count += 1
if img_array[x, y] == img_array[x - 1, y - 1]:
count += 1
if img_array[x, y] == img_array[x, y - 1]:
count += 1
if img_array[x, y] == img_array[x + 1, y - 1]:
count += 1
if count <= 2 and count > 0:
img_array[x, y] = 1
print("干扰线算法完成")
image.save('./deal_with_image/image_away_line{}.png'.format(num))
cls.image_orc('./deal_with_image/image_away_line.png'.format(num),num)
cls.Image_Center_decline(image,num)
@classmethod
@time_num.time_this_function
def Image_Center_decline(cls, image:str,num):
'''
中心降噪算法
:param image:
:param num:
:return:
'''
print("进行中心降噪,使用八邻域算法")
# 降噪算法,八邻域算法
pixdata = image.load()
wight, height = image.size
for x in range(1, wight - 1):
for y in range(1, height - 1):
count = 0
# 上方像素
if pixdata[x, y + 1] == 1:
count += 1
# 下方像素
if pixdata[x, y - 1] == 1:
count += 1
# 左方像素
if pixdata[x - 1, y] == 1:
count += 1
# 右方像素
if pixdata[x + 1, y] == 1:
count += 1
# 左上方像素
if pixdata[x - 1, y + 1] == 1:
count += 1
# 右上方像素
if pixdata[x + 1, y + 1] == 1:
count += 1
# 左下方像素
if pixdata[x - 1, y - 1] == 1:
count += 1
# 右下像素
if pixdata[x + 1, y - 1] == 1:
count += 1
if count > 5:
pixdata[x, y] = 1
print("完成降八邻域中心降噪算法")
image.save('./deal_with_image/image_decline{}.png'.format(num))
cls.image_orc('./deal_with_image/image_decline{}.png'.format(num),num)
cls.Image_frame_decline(image,num)
@classmethod
@time_num.time_this_function
def Image_frame_decline(cls, image:str,num):
'''
边框降噪算法
:param image:
:param num:
:return:
'''
print("正在进行边框去噪算法")
# 边框去噪算法
pixdata = image.load()
wight, height = image.size
for x in range(0, wight):
for y in range(0, height):
# 左边框
if x == 0:
if pixdata[x, y] == 0 and pixdata[x + 1, y] == 1:
pixdata[x, y] = 1
# 右边框
if x == wight - 1:
if pixdata[x, y] == 0 and pixdata[x - 1, y] == 1:
pixdata[x, y] = 1
# 下边框
if y == 0:
if pixdata[x, y] == 0 and pixdata[x, y + 1] == 1:
pixdata[x, y] = 1
# 上边框
if y == height - 1:
if pixdata[x, y] == 0 and pixdata[x, y - 1] == 1:
pixdata[x, y] = 1
print("完成边框去噪算法")
image.save('./deal_with_image/image_frame_decline{}.png'.format(num))
cls.image_orc('./deal_with_image/image_frame_decline{}.png'.format(num),num)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。