import os from PIL import Image,ImageDraw,ImageFont import xlrd # 要求录入学校信息的证书def zs_school(size, left, height, n, c, m1, d1, m2, d2, t): newfont = ImageFont.truetype(font="Songti.ttc", size=size) draw.text((600,height),n, font = newfont, fill= "black") draw.text((left,1700),c, font = newfont, fill= "black") draw.text((1750,1700),m1, font = newfont, fill= "black") draw.text((1900,1700),d1, font = newfont, fill= "black") draw.text((300,1850),m2, font = newfont, fill= "black") draw.text((550,1850),d2, font = newfont, fill= "black") draw.text((520,2150),t, font = newfont, fill= "black") # old_img.show() #运行显示 old_img.save(str(int(l))+n+".png") # 不要求录入学校信息的证书def zs(n, m1, d1, m2, d2, t):# 设置图片文字,字体类型,以及字体大小,颜色 newfont=ImageFont.truetype('Songti.ttc',100) draw.text((600,1380),n, font = newfont, fill= "black") draw.text((700,1700),m1, font = newfont, fill= "black") draw.text((850,1700),d1, font = newfont, fill= "black") draw.text((1170,1700),m2, font = newfont, fill= "black") draw.text((1380,1700),d2, font = newfont, fill= "black") draw.text((1400,2000),t, font = newfont, fill= "black") # old_img.show() #运行显示 old_img.save(str(int(l))+n+".png") # 导入 Excel 文件 data = xlrd.open_workbook('/Users/wangj676/Desktop/table1.xlsx') table = data.sheet_by_index(0) # 给各列表变量赋值 list = table.col_values(0)[0:] name = table.col_values(1)[0:] school = table.col_values(4)[0:] time = table.col_values(10)[0:] month1 = table.col_values(11)[0:] month2 = table.col_values(12)[0:] day1 = table.col_values(13)[0:] day2 = table.col_values(14)[0:] os.chdir("/Users/wangj676/Desktop/pics") # 循环生成新图片for i in range(0,113): l = list[i] n = name[i] s = school[i] m1 = str(int(month1[i])) m2 = str(int(month2[i])) d1 = str(int(day1[i])) d2 = str(int(day2[i])) t = str(int(time[i])) c=str(s)+" "+str(n) # 学校+“空格”+姓名# 判断是否要求录入学校信息:是if school[i]!="": old_img = Image.open("/Users/wangj676/Desktop/lxh.png") draw = ImageDraw.Draw(old_img) # 判断字符长度,以便设置字体大小和左边距if len(c)>=10: # 设置位置 length=len(c) left=600-22*length height = 1400 size = 80# 调用 zs_school() 函数 zs_school(size, left, height, n, c, m1, d1, m2, d2, t) else: # 设置位置 length=len(c) left=600-18*length height = 1380 size = 100# 调用 zs_school() 函数 zs_school(size, left, height, n, c, m1, d1, m2, d2, t) # 判断是否要求录入学校信息:否if school[i]=="": old_img = Image.open("/Users/wangj676/Desktop/lxh0.png") draw = ImageDraw.Draw(old_img) # 调用 zs() 函数 zs(n, c, m1, d1, m2, d2, t)
标签:draw,批量,python,text,newfont,black,font,调试,fill From: https://www.cnblogs.com/xkdn/p/17322530.html