Skip to content

Commit 90b4373

Browse files
committed
Merge pull request Show-Me-the-Code#183 from chenwanwan13/master
Liez's Python Exercise
2 parents 8c6be65 + 662884b commit 90b4373

File tree

25 files changed

+389
-0
lines changed

25 files changed

+389
-0
lines changed

Liez-python-code/0000/0000.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
from PIL import Image, ImageDraw, ImageFont
3+
4+
def add_num():
5+
im = Image.open('in.jpg')
6+
xsize, ysize = im.size
7+
draw = ImageDraw.Draw(im)
8+
font = ImageFont.truetype("arial.ttf", xsize // 3)
9+
draw.text((ysize // 5 * 4, 0), '3', (250,128,114), font)
10+
im.save('out.jpg')
11+
12+
add_num()

Liez-python-code/0000/in.jpg

46.2 KB
Loading

Liez-python-code/0000/out.jpg

35.4 KB
Loading

Liez-python-code/0001/0001.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# coding = utf-8
3+
__author__= 'liez'
4+
5+
import random
6+
7+
def make_number(num, length):
8+
str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
9+
a = []
10+
i = 0
11+
while i < num:
12+
numstr = ''
13+
for j in range(length):
14+
numstr += random.choice(str)
15+
if numstr not in a: #如果没重复
16+
a.append(numstr)
17+
i += 1
18+
print(numstr)
19+
20+
make_number(20,10)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
COsSx9umWf
2+
QKMSbDVj0G
3+
ARdycodYAf
4+
f8h0drSHJl
5+
e0T6lF6aO1
6+
ZB06xAlSnf
7+
SmcAqxYHqm
8+
209me2lsCl
9+
NnvmMeMgqd
10+
FqxLZVlzpC
11+
dj7luHI53s
12+
RytNv7yhTH
13+
bg7PDFmGE1
14+
I7r7S1mpWK
15+
agN2PYF3TI
16+
523YX6TdS8
17+
GmffcmyoYX
18+
MmZZ2pHieO
19+
gaHTmOVPDT
20+
6rgb3v2TJP
21+
22+

Liez-python-code/0002/0002.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# coding = utf-8
2+
__author__= 'liez'
3+
4+
import random
5+
import sqlite3
6+
7+
def make_number(num, length):
8+
str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
9+
a = []
10+
i = 0
11+
while i < num:
12+
numstr = ''
13+
for j in range(length):
14+
numstr += random.choice(str)
15+
if numstr not in a: #如果没重复
16+
a.append(numstr)
17+
i += 1
18+
print(a)
19+
return a
20+
21+
def save(a):
22+
try:
23+
connect = sqlite3.connect('codelist.db')
24+
except:
25+
print("failed")
26+
cur = connect.cursor()
27+
cur.execute('create table if not exists codes(code char(20) primary key)')
28+
for item in a:
29+
cur.execute('insert into codes values (?)', [item])
30+
print("success")
31+
connect.commit()
32+
cur.close()
33+
connect.close()
34+
35+
save(make_number(20, 10))

Liez-python-code/0004/0004.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# coding = utf-8
2+
__author__= 'liez'
3+
4+
import re
5+
def num(path):
6+
with open(path, 'r') as file:
7+
data=file.read()
8+
print(data)
9+
words=re.compile('[a-zA-Z0-9]+') #compile好像是必须用的,用来格式转换什么的,然后才能进行匹配之类的操作
10+
dict={}
11+
12+
for x in words.findall(data):
13+
if x not in dict:
14+
dict[x]=1
15+
else:
16+
dict[x]+=1
17+
18+
print(dict)
19+
20+
num('liez.txt')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I am liez.
2+
I am a player.
3+
I love programming.
4+
{'love': 1, 'I': 3, 'player': 1, 'programming': 1, 'a': 1, 'am': 2, 'liez': 1}
5+

Liez-python-code/0005/0005.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from PIL import Image
2+
import glob, os
3+
4+
def resize():
5+
for files in glob.glob('*.jpg'):
6+
filepath,filename = os.path.split(files) #分割文件名和路径名
7+
fname,fext = os.path.splitext(filename)
8+
im = Image.open(files)
9+
w,h = im.size
10+
if w > 640:
11+
x = w/640.0
12+
w = 640
13+
h = int(h/x)
14+
if h>1136:
15+
x = h/1136.0
16+
h = 1136
17+
w = int(w/x)
18+
print(w, h)
19+
im0 = im.resize((w,h),Image.ANTIALIAS)
20+
im0.save('0005'+filename)
21+
22+
resize()

Liez-python-code/0010/0010.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# codeing: utf-8
2+
3+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
4+
import string
5+
import random
6+
7+
8+
def get4char():
9+
return [random.choice(string.ascii_letters) for _ in range(4)] # 可以把‘_’换做任意字母,‘_’说明后续不用
10+
11+
12+
def getcolor():
13+
return random.randint(30, 100), random.randint(30, 100), random.randint(30, 100)
14+
15+
16+
def getpicture():
17+
width = 240
18+
height = 60
19+
20+
image = Image.new('RGB', (width, height), (180, 180, 180))
21+
font = ImageFont.truetype('arial.ttf', 40)
22+
23+
draw = ImageDraw.Draw(image)
24+
code = get4char()
25+
for ch in range(4):
26+
draw.text((60 * ch,30), code[ch], font=font, fill=getcolor())
27+
28+
for _ in range(random.randint(1500, 3000)):
29+
draw.point((random.randint(0, width), random.randint(0, height)), fill=getcolor())
30+
31+
image = image.filter(ImageFilter.BLUR)
32+
image.save("".join(code) + '.jpg')
33+
34+
35+
getpicture()

0 commit comments

Comments
 (0)