python练习小册1
6月18日
首先是一个关于图片水印的,通过使用PIL模块进行的,
from PIL import Image,ImageDraw,ImageFont def add_num(img): draw = ImageDraw.Draw(img) myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=40) fillcolor = "#ff0000" width, height = img.size draw.text((width-50, 5), '99', font=myfont, fill=fillcolor) img.save('result.jpg','jpeg') img.show return 0 if __name__ == '__main__': image = Image.open('1.jpg') add_num(image) image.show()
第二个便是随机生成200激活码
首先利用random模块进行对字母数字的随机选取
import random,string import uuid chars = string.ascii_letters +string.digits print(chars) s = "".join(random.choice(chars) for i in range(10)) ge = "".join(random.sample(chars,10)) #随机十位 print(s+" "+ge)
再通过嵌套函数的方式进行激活码的编写
import random,string import uuid chars = string.ascii_letters +string.digits print(chars) s = "".join(random.choice(chars) for i in range(10)) ge = "".join(random.sample(chars,10)) #随机十位 print(s+" "+ge) def generate(n,many): def getCode(n): return "".join(random.sample(chars, n)) gene = [getCode(n) for i in range(many)] #many个 return gene def wri(n,many,where): count = 1 for i in generate(n,many): with open(where,"a") as boom: boom.write(str(count).rjust(3)+" "+i+"\n") count += 1 if __name__ == '__main__': wri(20,200,"output.txt")