简介
该工具完成了如下功能:
1.将文字转换为手写体填入到模板文件中
2.自动将文字转换为盖章格式填入到模板文件中
3.字体格式可以替换
4.有配置文件进行扩展功能
功能模块
1.界面模块
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QLabel, QComboBox, QLineEdit, QPushButton, QVBoxLayout
from source import get_stamp_png
from source import get_pic_by_font
from source import config_read
font_path = "./font/SIMYOU.TTF"
font_size = 25
save_path = "./output/"
save_stamp_path = "./output/stamp/"
class TemplateGenerator(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle("模板盖章生成器V1.0")
self.setGeometry(300, 300, 400, 200)
layout = QVBoxLayout()
# 选择框
template_label = QLabel("选择模板:")
self.template_combobox = QComboBox()
self.template_combobox.addItem("口扫")
self.template_combobox.addItem("转诊")
layout.addWidget(template_label)
layout.addWidget(self.template_combobox)
#患者信息框
patient_name_label = QLabel("患者姓名:")
self.patient_name_edit = QLineEdit()
patient_pid_label = QLabel("患者PID:")
self.patient_pid_edit = QLineEdit()
patient_scan_id_label = QLabel("患者口扫ID:")
self.patient_scan_id_edit = QLineEdit()
layout.addWidget(patient_name_label)
layout.addWidget(self.patient_name_edit)
layout.addWidget(patient_pid_label)
layout.addWidget(self.patient_pid_edit)
layout.addWidget(patient_scan_id_label)
layout.addWidget(self.patient_scan_id_edit)
# 转出输入框
doctor_out_label = QLabel("转出医生姓名:")
self.doctor_out_edit = QLineEdit()
out_id_label = QLabel("转出医生DID:")
self.out_id_edit = QLineEdit()
clinic_out_label = QLabel("转出诊所名称:")
self.clinic_out_id_edit = QLineEdit()
layout.addWidget(doctor_out_label)
layout.addWidget(self.doctor_out_edit)
layout.addWidget(out_id_label)
layout.addWidget(self.out_id_edit)
layout.addWidget(clinic_out_label)
layout.addWidget(self.clinic_out_id_edit)
# 转入输入框
doctor_in_label = QLabel("转入医生姓名:")
self.doctor_in_edit = QLineEdit()
in_id_label = QLabel("转入医生DID:")
self.in_id_edit = QLineEdit()
clinic_in_label = QLabel("转出诊所名称:")
self.clinic_in_id_edit = QLineEdit()
layout.addWidget(doctor_in_label)
layout.addWidget(self.doctor_in_edit)
layout.addWidget(in_id_label)
layout.addWidget(self.in_id_edit)
layout.addWidget(clinic_in_label)
layout.addWidget(self.clinic_in_id_edit)
# 生成按钮
self.generate_button = QPushButton("生成")
self.generate_button.clicked.connect(self.generate_button_clicked)
layout.addWidget(self.generate_button)
self.setLayout(layout)
self.show()
def generate_button_clicked(self):
get_pic_by_font.delete_all_png()
template = self.template_combobox.currentText()#模板
patient_name = self.patient_name_edit.text()#患者姓名
patient_pid = self.patient_pid_edit.text()#患者PID
patient_scan_id = self.patient_scan_id_edit.text()#患者口扫ID
out_doctor_name = self.doctor_out_edit.text()#转出医生姓名
out_doctor_id = self.out_id_edit.text()#转出医生DID
out_clinic_name = self.clinic_out_id_edit.text()#转出诊所名称
in_doctor_name = self.doctor_in_edit.text()#转入医生姓名
in_doctor_id = self.in_id_edit.text()#转入医生DID
in_clinic_name = self.clinic_in_id_edit.text()#转入诊所名称
# 进行模板生成的逻辑处理,这里只是简单的打印输出
print("选择模板:", template)
print("患者姓名:",patient_name)
get_pic_by_font.generate_text_image(patient_name,font_path,font_size)
print("患者PID:",patient_pid)
get_pic_by_font.generate_text_image(patient_pid,font_path,font_size)
print("患者口扫ID:",patient_scan_id)
get_pic_by_font.generate_text_image(patient_scan_id,font_path,font_size)
print("转出医生姓名:", out_doctor_name)
print("转出医生DID:", out_doctor_id)
print("转出诊所名称:", out_clinic_name)
get_pic_by_font.generate_text_image(out_doctor_name,font_path,font_size)
get_pic_by_font.generate_text_image(out_doctor_id,font_path,font_size)
get_stamp_png.func_get_stamp_png(out_clinic_name)
print("转入医生姓名:", in_doctor_name)
print("转入医生DID:", in_doctor_id)
print("转入诊所名称:", in_clinic_name)
get_pic_by_font.generate_text_image(in_doctor_name,font_path,font_size)
get_pic_by_font.generate_text_image(in_doctor_id,font_path,font_size)
get_stamp_png.func_get_stamp_png(in_clinic_name)
if template == "口扫":
print("口扫盖章")
template_path = "./template/口扫.png"
output_ret_path = "./ret.png"
get_pic_by_font.func_seal(template_path,save_path+patient_name+".png",output_ret_path,190,270)#患者名字盖章
get_pic_by_font.func_seal(output_ret_path,save_path+patient_pid+".png",output_ret_path,760,270)#PID盖章
get_pic_by_font.func_seal(output_ret_path,save_path+patient_scan_id+".png",output_ret_path,1350,270)#口扫ID盖章
get_pic_by_font.func_seal(output_ret_path,save_path+out_doctor_name+".png",output_ret_path,190,710)#转出医生姓名盖章
get_pic_by_font.func_seal(output_ret_path,save_path+out_doctor_id+".png",output_ret_path,640,710)#转出医生DID盖章
get_pic_by_font.func_seal(output_ret_path,save_stamp_path+out_clinic_name+".png",output_ret_path,1190,770)#转出诊所名称盖章
get_pic_by_font.func_seal(output_ret_path,save_path+in_doctor_name+".png",output_ret_path,190,1090)#转入医生姓名盖章
get_pic_by_font.func_seal(output_ret_path,save_path+in_doctor_id+".png",output_ret_path,660,1090)#转入医生DID盖章
get_pic_by_font.func_seal(output_ret_path,save_stamp_path+in_clinic_name+".png",output_ret_path,1250,1100)#转入诊所名称盖章
get_pic_by_font.func_seal(output_ret_path,save_path+patient_name+".png",output_ret_path,170,1690)#患者名字盖章
get_pic_by_font.func_seal(output_ret_path,save_path+patient_name+".png",output_ret_path,710,1690)#患者名字盖章
QMessageBox.warning(None, "Warning", "盖章完毕,文件名为ret.png")
elif template == "转诊":
print("转诊盖章")
template_path = "./template/转诊.png"
if __name__ == "__main__":
my_config = config_read.read_config()
font_path = my_config.get('Font','style')
font_size = int(my_config.get('Font','size'))
print(font_path)
print(font_size)
app = QApplication(sys.argv)
window = TemplateGenerator()
sys.exit(app.exec_())
2.配置文件模块
import configparser
import os
# 读取配置文件
def read_config():
#root_dir = os.path.dirname(os.path.dirname(__file__)) # # 获取当前文件所在目录
#config_dir = os.path.join(root_dir, './config', 'config.ini') # 组装config.ini路径,也可以直接写配置文件的具体路径,不用自动获取
cf = configparser.ConfigParser()
cf.read("./config/config.ini", encoding="utf-8") # 读取config.ini
return cf
3.文本转换手写字体图片模块
from PIL import Image, ImageDraw, ImageFont
import os
import glob
def generate_text_image(text, font_path, font_size):
# 创建一个空白的图片
#print(text.length()*100)
if len(text) == 0:
return
image = Image.new('RGB', (len(text)*25+20, 40), color='white')
draw = ImageDraw.Draw(image)
# 加载手写字体
font = ImageFont.truetype(font_path, font_size)
# 在图片上绘制文本
draw.text((10, 10), text, font=font, fill='black')
# 保存生成的图片
image.save("./output/"+text+".png")
# 指定目录
directory = "./output"
def delete_all_png():
# 获取指定目录中所有后缀名为".png"的文件列表
png_files = glob.glob(os.path.join(directory, "*.png"))
# 遍历文件列表,逐个删除文件
for png_file in png_files:
try:
os.remove(png_file)
print(f"已删除文件:{png_file}")
except OSError as e:
print(f"删除文件时出错:{e}")
def func_seal(background,stamp,ret_name,x,y):
'''
print("seal satrt")
print(background)
print(stamp)
print(ret_name)
'''
imageA = Image.open(stamp)#章
imageB = Image.open(background)#表
# 将图像A的背景色设置为透明
imageA = imageA.convert("RGBA")
datas = imageA.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
imageA.putdata(newData)
# 在图像B上粘贴图像A,并将B的背景色设置为A的透明区域的背景色
imageB.paste(imageA, (x, y), imageA)
newImage = Image.new("RGBA", imageB.size, (255, 255, 255, 255))
newImage.paste(imageB, (0, 0), imageB)
# 保存新图像
newImage.save(ret_name, "PNG")
return ret_name
4.文本生成盖章内容模块
import requests
import urllib.parse
from PIL import Image
def func_get_stamp_png(name):
# 定义API的地址
if len(name) == 0:
return
out_put_name = "./output/stamp/"+name+".png"
url = "http://www.yinzhang8.com.cn/seal/index.php?name="
url += urllib.parse.quote(name)
url += "&type=1"
# 发送HTTP请求
response = requests.get(url)
print(response.status_code)
#print(response.text)
print(type(response))
# 将返回的二进制数据保存为本地文件
with open(out_put_name, "wb") as f:
f.write(response.content)
# 打开图片文件
img = Image.open(out_put_name)
# 获取图片的宽度和高度
width, height = img.size
# 计算需要裁剪的高度
crop_height = int(height * 0.1)
# 裁剪图片
cropped_img = img.crop((0, 0, width, height - crop_height))
# 覆盖保存原始文件
cropped_img.save(out_put_name)