python 验证码获取后处理降噪灰度保存
Posted winstonsias
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 验证码获取后处理降噪灰度保存相关的知识,希望对你有一定的参考价值。
def convert_image(self): image_obj = Image.open(self.captcha_path)# 获取验证码 img = image_obj.convert("L") # 转灰度 pixdata = img.load() w, h = img.size threshold = 160 # 遍历所有像素,大于阈值的为黑色 for y in range(h): for x in range(w): if pixdata[x, y] < threshold: pixdata[x, y] = 0 else: pixdata[x, y] = 255 return img def process_image(self): images = self.convert_image() data = images.getdata() w, h = images.size black_point = 0 for x in range(1, w - 1): for y in range(1, h - 1): mid_pixel = data[w * y + x] # 中央像素点像素值 if mid_pixel < 50: # 找出上下左右四个方向像素点像素值 top_pixel = data[w * (y - 1) + x] left_pixel = data[w * y + (x - 1)] down_pixel = data[w * (y + 1) + x] right_pixel = data[w * y + (x + 1)] # 判断上下左右的黑色像素点总个数 if top_pixel < 10: black_point += 1 if left_pixel < 10: black_point += 1 if down_pixel < 10: black_point += 1 if right_pixel < 10: black_point += 1 if black_point < 1: images.putpixel((x, y), 255) black_point = 0 images.save(self.process_captcha_path) #return images
以上是关于python 验证码获取后处理降噪灰度保存的主要内容,如果未能解决你的问题,请参考以下文章