最近需要做很多张体验券,体验券上要有不同的ID号,ID共8位数,是随机生成的,同时我们要有个excel表能对照生成了哪些ID,思路很简单,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#-*- coding:utf8 -*- import PIL import math import random import csv from PIL import ImageFont from PIL import Image from PIL import ImageDraw csvfile = file('number.csv', 'wb') writer = csv.writer(csvfile) imageFile = "huodong.jpg" im1 = Image.open(imageFile) font = ImageFont.truetype('simsun.ttc', 32) for i in range(30): im1 = Image.open(imageFile) numberstr = str(random.randint(0, 99999999)).zfill(8) print numberstr data = [str(i+1), str(numberstr)] print data writer.writerow(data) txt = 'ID:' + numberstr draw = ImageDraw.Draw(im1) draw.text((500, 173), unicode(txt, 'UTF-8'), fill='#000000', font=font) draw = ImageDraw.Draw(im1) im1.save(numberstr + ".jpg") csvfile.close() |