Skip to content

Commit ff49425

Browse files
committed
Python
1 parent 1819c49 commit ff49425

11 files changed

Lines changed: 20229 additions & 4 deletions

python_base/.ipynb_checkpoints/python2__关键字,容器及访问,循环-checkpoint.ipynb

Lines changed: 941 additions & 0 deletions
Large diffs are not rendered by default.

python_base/.ipynb_checkpoints/python4__面向对象-checkpoint.ipynb

Lines changed: 945 additions & 0 deletions
Large diffs are not rendered by default.

python_base/.ipynb_checkpoints/python7__Numpy高效数据处理-checkpoint.ipynb

Lines changed: 3593 additions & 0 deletions
Large diffs are not rendered by default.

python_base/.ipynb_checkpoints/python8__Padas表格处理-checkpoint.ipynb

Lines changed: 2692 additions & 0 deletions
Large diffs are not rendered by default.

python_base/ibm.txt

Lines changed: 5474 additions & 0 deletions
Large diffs are not rendered by default.

python_base/python2__关键字,容器及访问,循环.ipynb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,76 @@
757757
"fibonacci(10)"
758758
]
759759
},
760+
{
761+
"cell_type": "code",
762+
"execution_count": 4,
763+
"metadata": {},
764+
"outputs": [
765+
{
766+
"name": "stdout",
767+
"output_type": "stream",
768+
"text": [
769+
"total 48.84060068717216\n"
770+
]
771+
},
772+
{
773+
"data": {
774+
"text/plain": [
775+
"['2/1',\n",
776+
" '3/2',\n",
777+
" '5/3',\n",
778+
" '8/5',\n",
779+
" '13/8',\n",
780+
" '21/13',\n",
781+
" '34/21',\n",
782+
" '55/34',\n",
783+
" '89/55',\n",
784+
" '144/89',\n",
785+
" '233/144',\n",
786+
" '377/233',\n",
787+
" '610/377',\n",
788+
" '987/610',\n",
789+
" '1597/987',\n",
790+
" '2584/1597',\n",
791+
" '4181/2584',\n",
792+
" '6765/4181',\n",
793+
" '10946/6765',\n",
794+
" '17711/10946',\n",
795+
" '28657/17711',\n",
796+
" '46368/28657',\n",
797+
" '75025/46368',\n",
798+
" '121393/75025',\n",
799+
" '196418/121393',\n",
800+
" '317811/196418',\n",
801+
" '514229/317811',\n",
802+
" '832040/514229',\n",
803+
" '1346269/832040',\n",
804+
" '2178309/1346269']"
805+
]
806+
},
807+
"execution_count": 4,
808+
"metadata": {},
809+
"output_type": "execute_result"
810+
}
811+
],
812+
"source": [
813+
"def fibonacci(n):\n",
814+
" fib=[]\n",
815+
" fm=1 # 分母\n",
816+
" fz=2 # 分子\n",
817+
" i=2\n",
818+
" total=0\n",
819+
" for i in range(1,n+1):\n",
820+
" total += fz/fm\n",
821+
" fib.append('%d/%d'%(fz,fm))\n",
822+
" fm,fz=fz,fm+fz\n",
823+
" i+=1\n",
824+
" print('total',total)\n",
825+
" return fib\n",
826+
"\n",
827+
"fibonacci(30)"
828+
]
829+
},
760830
{
761831
"cell_type": "code",
762832
"execution_count": 92,

python_base/python4__面向对象.ipynb

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,17 +838,123 @@
838838
},
839839
{
840840
"cell_type": "code",
841-
"execution_count": null,
841+
"execution_count": 11,
842842
"metadata": {},
843843
"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+
]
845908
},
846909
{
847910
"cell_type": "code",
848-
"execution_count": null,
911+
"execution_count": 6,
849912
"metadata": {},
850913
"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+
]
852958
},
853959
{
854960
"cell_type": "code",

0 commit comments

Comments
 (0)