ColorPreview.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. # -*- coding: UTF-8 -*-
  2. from PIL import Image,ImageDraw,ImageFont,ImageFilter
  3. bgcolor = (28, 44, 97)
  4. forecolor = [
  5. (228,0,255),(255,0,0),(0,255,255),(255,255,0),(189,75,110),
  6. (75,189,88),(221,140,45),(45,165,221),(0,255,0),
  7. (255,126,217),(126,255,196),(255,205,126),
  8. (255,126,126),(181,126,255),(126,162,255),(232,255,126)
  9. ]
  10. text = u"测试Example"
  11. size = (400,400)
  12. def create_strs(draw, font_type, font_size, width, height, fg_color, strs, ofs):
  13. '''绘制验证码字符'''
  14. '''生成给定长度的字符串,返回列表格式'''
  15. #c_chars = random.sample(chars, length)
  16. # c_chars=random.sample(rjs, length)
  17. # strs = ' %s ' % ' '.join(c_chars) # 每个字符前后以空格隔开
  18. font = ImageFont.truetype(font_type, font_size)
  19. font_width, font_height = font.getsize(strs)
  20. draw.text((10,ofs*font_height), strs, font=font, fill=fg_color)
  21. img = Image.new('RGB', size, bgcolor)
  22. draw = ImageDraw.Draw(img)
  23. for i,v in enumerate(forecolor):
  24. create_strs(draw,"FZCQJW.ttf",16,size[0],size[1],v,text,i)
  25. f = open("test.png","wb")
  26. img.save(f, "PNG")
  27. f.close()