Python随机生成图片

 

Python生成随机验证码,需要使用PIL模块.

安装:

  1. 1 pip install pillow

1. 创建图片

  1. 1 from PIL import Image
  2. 2 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  3. 3
  4. 4 # 在图片查看器中打开
  5. 5 # img.show()
  6. 6
  7. 7 # 保存在本地
  8. 8 with open('code.png','wb') as f:
  9. 9 img.save(f,format='png'

 

2. 创建画笔,用于在图片上画任意内容

  1. 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  2. 2 draw = ImageDraw.Draw(img, mode='RGB')

 

3. 画点

  1. 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  2. 2 draw = ImageDraw.Draw(img, mode='RGB')
  3. 3 # 第一个参数:表示坐标
  4. 4 # 第二个参数:表示颜色
  5. 5 draw.point([100, 100], fill="red")
  6. 6 draw.point([300, 300], fill=(255, 255, 255))
 

4. 画线

  1. 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  2. 2 draw = ImageDraw.Draw(img, mode='RGB')
  3. 3 # 第一个参数:表示起始坐标和结束坐标
  4. 4 # 第二个参数:表示颜色
  5. 5 draw.line((100,100,100,300), fill='red')
  6. 6 draw.line((100,100,300,100), fill=(255, 255, 255))
 

5. 画圆

  1. 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  2. 2 draw = ImageDraw.Draw(img, mode='RGB')
  3. 3 # 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
  4. 4 # 第二个参数:表示开始角度
  5. 5 # 第三个参数:表示结束角度
  6. 6 # 第四个参数:表示颜色
  7. 7 draw.arc((100,100,300,300),0,90,fill="red")

6. 写文本

  1. 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  2. 2 draw = ImageDraw.Draw(img, mode='RGB')
  3. 3 # 第一个参数:表示起始坐标
  4. 4 # 第二个参数:表示写入内容
  5. 5 # 第三个参数:表示颜色
  6. 6 draw.text([0,0],'python',"red")

7. 特殊字体文字

  1. 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
  2. 2 draw = ImageDraw.Draw(img, mode='RGB')
  3. 3 # 第一个参数:表示字体文件路径
  4. 4 # 第二个参数:表示字体大小
  5. 5 font = ImageFont.truetype("kumo.ttf", 28)
  6. 6 # 第一个参数:表示起始坐标
  7. 7 # 第二个参数:表示写入内容
  8. 8 # 第三个参数:表示颜色
  9. 9 # 第四个参数:表示颜色
  10. 10 draw.text([0, 0], 'python', "red", font=font)
  1. 1 import random
  2. 2
  3. 3 def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
  4. 4 code = []
  5. 5 img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
  6. 6 draw = ImageDraw.Draw(img, mode='RGB')
  7. 7
  8. 8 def rndChar():
  9. 9 """
  10. 10 生成随机字母
  11. 11 :return:
  12. 12 """
  13. 13 return chr(random.randint(65, 90))
  14. 14
  15. 15 def rndColor():
  16. 16 """
  17. 17 生成随机颜色
  18. 18 :return:
  19. 19 """
  20. 20 return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
  21. 21
  22. 22 # 写文字
  23. 23 font = ImageFont.truetype(font_file, font_size)
  24. 24 for i in range(char_length):
  25. 25 char = rndChar()
  26. 26 code.append(char)
  27. 27 h = random.randint(0, 4)
  28. 28 draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
  29. 29
  30. 30 # 写干扰点
  31. 31 for i in range(40):
  32. 32 draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
  33. 33
  34. 34 # 写干扰圆圈
  35. 35 for i in range(40):
  36. 36 draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
  37. 37 x = random.randint(0, width)
  38. 38 y = random.randint(0, height)
  39. 39 draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
  40. 40
  41. 41 # 画干扰线
  42. 42 for i in range(5):
  43. 43 x1 = random.randint(0, width)
  44. 44 y1 = random.randint(0, height)
  45. 45 x2 = random.randint(0, width)
  46. 46 y2 = random.randint(0, height)
  47. 47
  48. 48 draw.line((x1, y1, x2, y2), fill=rndColor())
  49. 49
  50. 50 img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
  51. 51 return img,''.join(code)
  52. 52
  53. 53
  54. 54 if __name__ == '__main__':
  55. 55 # 1. 直接打开
  56. 56 # img,code = check_code()
  57. 57 # img.show()
  58. 58
  59. 59 # 2. 写入文件
  60. 60 # img,code = check_code()
  61. 61 # with open('code.png','wb') as f:
  62. 62 # img.save(f,format='png')
  63. 63
  64. 64 # 3. 写入内存(Python3)
  65. 65 # from io import BytesIO
  66. 66 # stream = BytesIO()
  67. 67 # img.save(stream, 'png')
  68. 68 # stream.getvalue()
  69. 69
  70. 70 # 4. 写入内存(Python2)
  71. 71 # import StringIO
  72. 72 # stream = StringIO.StringIO()
  73. 73 # img.save(stream, 'png')
  74. 74 # stream.getvalue()
  75. 75
  76. 76 pass

链接:https://pan.baidu.com/s/1yMYEC1SwI3IoRGeHFRxoYQ
提取码:m6ww(注意:字体文件下载)