|
838 | 838 | }, |
839 | 839 | { |
840 | 840 | "cell_type": "code", |
841 | | - "execution_count": null, |
| 841 | + "execution_count": 11, |
842 | 842 | "metadata": {}, |
843 | 843 | "outputs": [], |
844 | | - "source": [] |
| 844 | + "source": [ |
| 845 | + "from PIL import Image\n", |
| 846 | + "from PIL import ImageDraw\n", |
| 847 | + "from PIL import ImageFont\n", |
| 848 | + "import random\n", |
| 849 | + "\n", |
| 850 | + "def getRandomColor():\n", |
| 851 | + " '''获取一个随机颜色(r,g,b)格式的'''\n", |
| 852 | + " c1 = random.randint(0,255)\n", |
| 853 | + " c2 = random.randint(0,255)\n", |
| 854 | + " c3 = random.randint(0,255)\n", |
| 855 | + " return (c1,c2,c3)\n", |
| 856 | + "\n", |
| 857 | + "\n", |
| 858 | + "def getRandomStr():\n", |
| 859 | + " '''获取一个随机字符串,每个字符的颜色也是随机的'''\n", |
| 860 | + " random_num = str(random.randint(0, 9))\n", |
| 861 | + " random_low_alpha = chr(random.randint(97, 122))\n", |
| 862 | + " random_upper_alpha = chr(random.randint(65, 90))\n", |
| 863 | + " random_char = random.choice([random_num, random_low_alpha, random_upper_alpha])\n", |
| 864 | + "\n", |
| 865 | + " return random_char\n", |
| 866 | + "\n", |
| 867 | + "\n", |
| 868 | + "# 获取一个Image对象,参数分别是RGB模式。宽150,高30,随机颜色\n", |
| 869 | + "image = Image.new('RGB',(150,30),getRandomColor())\n", |
| 870 | + "\n", |
| 871 | + "# 获取一个画笔对象,将图片对象传过去\n", |
| 872 | + "draw = ImageDraw.Draw(image)\n", |
| 873 | + "\n", |
| 874 | + "# 获取一个font字体对象参数是ttf的字体文件的目录,以及字体的大小\n", |
| 875 | + "font=ImageFont.load_default().font\n", |
| 876 | + "\n", |
| 877 | + "\n", |
| 878 | + "for i in range(5):\n", |
| 879 | + " # 循环5次,获取5个随机字符串\n", |
| 880 | + " random_char = getRandomStr()\n", |
| 881 | + "\n", |
| 882 | + " # 在图片上一次写入得到的随机字符串,参数是:定位,字符串,颜色,字体\n", |
| 883 | + " draw.text((10+i*30, 0),random_char , getRandomColor(), font=font)\n", |
| 884 | + "\n", |
| 885 | + "\n", |
| 886 | + "# 噪点噪线\n", |
| 887 | + "width=150\n", |
| 888 | + "height=30\n", |
| 889 | + "# 划线\n", |
| 890 | + "for i in range(5):\n", |
| 891 | + " x1=random.randint(0,width)\n", |
| 892 | + " x2=random.randint(0,width)\n", |
| 893 | + " y1=random.randint(0,height)\n", |
| 894 | + " y2=random.randint(0,height)\n", |
| 895 | + " draw.line((x1,y1,x2,y2),fill=getRandomColor())\n", |
| 896 | + "\n", |
| 897 | + "# 画点\n", |
| 898 | + "for i in range(30):\n", |
| 899 | + " draw.point([random.randint(0, width), random.randint(0, height)], fill=getRandomColor())\n", |
| 900 | + " x = random.randint(0, width)\n", |
| 901 | + " y = random.randint(0, height)\n", |
| 902 | + " draw.arc((x, y, x + 4, y + 4), 0, 90, fill=getRandomColor())\n", |
| 903 | + "\n", |
| 904 | + "# 保存到硬盘,名为test.png格式为png的图片\n", |
| 905 | + "image.save(open('./test.png', 'wb'), 'png')\n", |
| 906 | + "\n" |
| 907 | + ] |
845 | 908 | }, |
846 | 909 | { |
847 | 910 | "cell_type": "code", |
848 | | - "execution_count": null, |
| 911 | + "execution_count": 6, |
849 | 912 | "metadata": {}, |
850 | 913 | "outputs": [], |
851 | | - "source": [] |
| 914 | + "source": [ |
| 915 | + "import random\n", |
| 916 | + "import string\n", |
| 917 | + "import sys\n", |
| 918 | + "import math\n", |
| 919 | + "from PIL import Image,ImageDraw,ImageFont,ImageFilter\n", |
| 920 | + "\n", |
| 921 | + "# 生成几位数的验证码\n", |
| 922 | + "number = 4\n", |
| 923 | + "#生成验证码图片的高度和宽度\n", |
| 924 | + "size = (129,53)\n", |
| 925 | + "#背景颜色,默认为白色\n", |
| 926 | + "bgcolor = (random.randint(0,255),random.randint(0,255),random.randint(0,255))\n", |
| 927 | + "#字体颜色,默认为蓝色\n", |
| 928 | + "fontcolor = (0,0,0)\n", |
| 929 | + "#干扰线颜色。默认为红色\n", |
| 930 | + "linecolor = (255,0,0)\n", |
| 931 | + "#加入干扰线条数的上下限\n", |
| 932 | + "line_number = (1,5)\n", |
| 933 | + "\n", |
| 934 | + "#随机生成一个字符串\n", |
| 935 | + "source = ['0','1','2','3','4','5','6','7','8','9','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J', 'K','L', 'M', 'N','O','P','Q','R','S', 'T', 'U', 'V', 'W', 'Z','X', 'Y']\n", |
| 936 | + "gene_text = ''.join(random.sample(source,number))#number是生成验证码的位数\n", |
| 937 | + "\n", |
| 938 | + "#绘制干扰线\n", |
| 939 | + "width,height = size\n", |
| 940 | + "begin = (0, random.randint(0, height))\n", |
| 941 | + "end = (74, random.randint(0, height))\n", |
| 942 | + "image = Image.new('RGBA',(width,height),bgcolor) #创建图片\n", |
| 943 | + "draw = ImageDraw.Draw(image) #创建画笔\n", |
| 944 | + "gene_line = draw.line([begin, end], fill = linecolor,width=3)\n", |
| 945 | + "\n", |
| 946 | + "#生成验证码\n", |
| 947 | + "width,height = size #宽和高\n", |
| 948 | + "font = ImageFont.load_default().font #验证码默认系统字体\n", |
| 949 | + "# font = ImageFont.truetype(\"C:/Windows/Fonts/micross.ttf\", 40)\n", |
| 950 | + "text = gene_text\n", |
| 951 | + "font_width, font_height = font.getsize(text)\n", |
| 952 | + "draw.text(((width - font_width) / number, (height - font_height) / number),text,font= font,fill=fontcolor) #填充字符串\n", |
| 953 | + "image = image.transform((width+30,height+10), Image.AFFINE, (1,-0.3,0,-0.1,1,0),Image.BILINEAR) #创建扭曲\n", |
| 954 | + "image = image.filter(ImageFilter.EDGE_ENHANCE_MORE) #滤镜,边界加强\n", |
| 955 | + "# image.save('yanzhengma.png') #保存验证码图片\n", |
| 956 | + "image.show()" |
| 957 | + ] |
852 | 958 | }, |
853 | 959 | { |
854 | 960 | "cell_type": "code", |
|
0 commit comments