import time from PIL import Image,ImageDraw,ImageFont import os import cv2 if not os.path.exists("\\studentpic\\"): os.mkdir("\\studentpic\\") if not os.path.exists("\\studentcards\\"): os.mkdir("\\studentcards\\") photopath = ".\\studentpic\\" files=[] for file in os.listdir(photopath): if file.endswith(".jpg"): files.append(photopath+file) print(files) for file in files: img = cv2.imread(file) cv2.imshow("windows image",img) cv2.waitKey(5000) cv2.destroyAllWindows() name = input("请你输入学生的姓名:") stuclass = input("请你输入学生的班级:") tempimg = Image.open("./photopath/foot_template.jpg") imgdraw = ImageDraw.Draw(tempimg) font = ImageFont.truetype("./photopath/simhei.ttf",80) font2 = ImageFont.truetype("./photopath/STLITI.ttf",80) imgdraw.text((350,860),name,font=font2,fill=(0,0,255),stroke_width=2,stroke_fill=(255,255,255)) imgdraw.text((380,952),stuclass,font=font,fill=(0,0,255),stroke_width=2,stroke_fill=(255,255,255)) # tempimgheith = tempimg.height # tempimgwidth = tempimg.width # print(tempimgwidth,tempimgheith) photoimg = Image.open(file) tempimg.paste(photoimg,(266,377)) tempimg.save(f"studentcards/{stuclass}班{name}.jpg")
功能概述
这段 Python 代码的主要目的是从指定的文件夹(studentpic
)中读取学生的照片文件(以.jpg
结尾),在屏幕上显示每张照片一段时间,然后让用户输入对应学生的姓名和班级信息,接着将这些信息添加到一个模板图片(foot_template.jpg
)上相应的位置,并把学生照片粘贴到模板图片的特定位置,最后将生成的带有学生信息和照片的新图片保存到另一个文件夹(studentcards
)中,文件名按照班级和姓名来命名。
import time
from PIL import Image, ImageDraw, ImageFont
import os
import cv2
这里导入了处理时间的time
模块,用于图像操作的PIL
库(Image
、ImageDraw
、ImageFont
用于打开、绘制和设置字体),以及用于操作系统相关操作的os
模块和用于计算机视觉相关操作(这里主要是读取和显示图像)的cv2
(OpenCV 库)。
创建文件夹(如果不存在):
if not os.path.exists("\\studentpic\\"):
os.mkdir("\\studentpic\\")
if not os.path.exists("\\studentcards\\"):
os.mkdir("\\studentcards\\")
这段代码检查studentpic
和studentcards
这两个文件夹是否存在,如果不存在就创建它们。不过这里的路径写法在不同操作系统下可能有问题,在 Windows 下应该使用r"\\studentpic\\"
这样的原始字符串表示法来避免转义字符带来的歧义,在 Linux 等系统下路径分隔符应该是/
而不是\\
。
获取照片文件列表:
photopath = ".\\studentpic\\"
files = []
for file in os.listdir(photopath):
if file.endswith(".jpg"):
files.append(photopath + file)
print(files)
首先定义了照片所在的路径photopath
,然后通过遍历该路径下的所有文件,筛选出以.jpg
结尾的文件,并将它们的完整路径添加到files
列表中,最后打印出这个列表。
处理每张照片:
for file in files:
img = cv2.imread(file)
cv2.imshow("windows image", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
name = input("请你输入学生的姓名:")
stuclass = input("请你输入学生的班级:")
tempimg = Image.open("./photopath/foot_template.jpg")
imgdraw = ImageDraw.Draw(tempimg)
font = ImageFont.truetype("./photopath/simhei.ttf", 80)
font2 = ImageFont.truetype("./photopath/STLITI.ttf", 80)
imgdraw.text((350, 860), name, font=font2, fill=(0, 0, 255), stroke_width=2, stroke_fill=(255, 255, 255))
imgdraw.text((380, 952), stuclass, font=font, fill=(0, 0, 255), stroke_width=2, stroke_fill=(255, 255, 255))
photoimg = Image.open(file)
tempimg.paste(photoimg, (266, 377))
tempimg.save(f"studentcards/{stuclass}班{name}.jpg")
- 对于
files
列表中的每张照片文件:- 首先使用
cv2.imread
读取照片并通过cv2.imshow
显示出来,然后使用cv2.waitKey(5000)
让图片在屏幕上显示 5 秒钟,之后使用cv2.destroyAllWindows
关闭显示窗口。这里需要注意的是,如果在一些环境下(比如某些 Jupyter Notebook 环境),cv2.imshow
可能无法正常工作,需要进行额外的配置。 - 接着通过
input
函数让用户分别输入学生的姓名和班级信息。 - 然后打开模板图片
tempimg = Image.open("./photopath/foot_template.jpg")
,这里的路径写法可能有误,应该根据实际情况调整,可能是"./studentpic/foot_template.jpg"
之类的正确路径。之后创建一个ImageDraw
对象用于在图片上绘制文字。 - 再分别设置两种字体(可能是用于显示姓名和班级的不同字体风格),并使用
imgdraw.text
方法将学生姓名和班级信息绘制到模板图片的指定位置上,设置了文字的颜色、描边宽度和描边颜色等属性。 - 之后再次打开当前正在处理的学生照片
photoimg = Image.open(file)
,并使用tempimg.paste
将照片粘贴到模板图片的指定位置。 - 最后使用
tempimg.save
将生成的带有学生信息和照片的新图片保存到studentcards
文件夹下,文件名按照班级和姓名来命名。
- 首先使用