编写绘制文字的方法
def draw_text(self,draw, text, font, captcha_width, captcha_height, spacing=20): ''' 在图片上绘制传入的字符 :param draw: 画笔对象 :param text: 绘制的所有字符 :param font: 字体对象 :param captcha_width: 验证码的宽度 :param captcha_height: 验证码的高度 :param spacing: 每个字符的间隙 :return: ''' # 得到这一窜字符的高度和宽度 text_width, text_height = font.getsize(text) # 得到每个字体的大概宽度 every_value_width = int(text_width / 4) # 这一窜字符的总长度 text_length = len(text) # 每两个字符之间拥有间隙,获取总的间隙 total_spacing = (text_length-1) * spacing if total_spacing + text_width >= captcha_width: raise ValueError("字体加中间空隙超过图片宽度!") # 获取第一个字符绘制位置 start_width = int( (captcha_width - text_width - total_spacing) / 2 ) start_height = int( (captcha_height - text_height) / 2 ) # 依次绘制每个字符 for value in text: position = start_width, start_height print(position) # 绘制text draw.text(position, value, font=font, fill=self.get_font_color()) # 改变下一个字符的开始绘制位置 start_width = start_width + every_value_width + spacing绘制线条的方法
def draw_line(self, draw, captcha_width, captcha_height): ''' 绘制线条 :param draw: 画笔对象 :param captcha_width: 验证码的宽度 :param captcha_height: 验证码的高度 :return: ''' # 随机获取开始位置的坐标 begin = (random.randint(0,captcha_width/2), random.randint(0, captcha_height)) # 随机获取结束位置的坐标 end = (random.randint(captcha_width/2,captcha_width), random.randint(0, captcha_height)) draw.line([begin, end], fill=self.get_line_color())绘制小圆点
def draw_point(self, draw, point_chance, width, height): ''' 绘制小圆点 :param draw: 画笔对象 :param point_chance: 绘制小圆点的几率 概率为 point_chance/100 :param width: 验证码宽度 :param height: 验证码高度 :return: ''' # 按照概率随机绘制小圆点 for w in range(width): for h in range(height): tmp = random.randint(0, 100) if tmp < point_chance: draw.point((w, h), fill=self.get_line_color())制作验证码
def make_captcha(self): # 获取验证码的宽度, 高度 width, height = self.captcha_size # 生成一张图片 captcha = Image.new('RGB',self.captcha_size,self.background_color) # 获取字体对象 font = ImageFont.truetype('simkai.ttf',self.font_size) # 获取画笔对象 draw = ImageDraw.Draw(captcha) # 得到绘制的字符 text = self.get_text( # 绘制字符 self.draw_text(draw, text, font, width, height) # 绘制线条 for i in range(self.line_number): self.draw_line(draw, width, height) # 绘制小圆点 10是概率 10/100, 10%的概率 self.draw_point(draw,10,width,height) # 保存图片 captcha.save('captcha',format=self.format) # 显示图片 captcha.show()这样,我们就生成了我们的图片验证码了,效果图.
代码已全部上传至Github:https://github.com/MiracleYoung/You-are-Pythonista/tree/master/PythonExercise/App/captcha_project