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