【自动化】批量生成的带有学生信息和照片的学生证

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库(ImageImageDrawImageFont用于打开、绘制和设置字体),以及用于操作系统相关操作的os模块和用于计算机视觉相关操作(这里主要是读取和显示图像)的cv2(OpenCV 库)。

创建文件夹(如果不存在)

if not os.path.exists("\\studentpic\\"):
    os.mkdir("\\studentpic\\")
if not os.path.exists("\\studentcards\\"):
    os.mkdir("\\studentcards\\")

这段代码检查studentpicstudentcards这两个文件夹是否存在,如果不存在就创建它们。不过这里的路径写法在不同操作系统下可能有问题,在 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文件夹下,文件名按照班级和姓名来命名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青龙摄影

你的鼓励是我创作的动力,支持下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值