# _*_ coding:utf-8 _*_
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import datetime
import os
import sys
import xlrd
import io
from PIL import Image, ImageDraw, ImageFont
from wand.image import Image as WI
from PyQt5 import QtGui
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QProgressBar, QSpinBox, QAbstractSpinBox, \
QRadioButton, QButtonGroup, QDialog, QSizePolicy
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtCore import QSize, QThread, pyqtSignal, Qt
import logging
from logging.handlers import RotatingFileHandler
import winreg
from psd_tools import PSDImage
from psd_tools.constants import ColorMode
LOG_PATH = 'logs' # 设置log路径
LOG_FILE = 'log.log' # 设置log文件名
PNG_PATH = 'png'
# 设置根路径为起始位置
g_logger = logging.getLogger(__name__)
# 生成log指定路径
if os.path.exists(LOG_PATH):
pass
else:
os.mkdir(LOG_PATH)
if os.path.exists(PNG_PATH):
pass
else:
os.mkdir(PNG_PATH)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
# file_handler = logging.FileHandler("%s/%s" % (LOG_PATH, LOG_FILE), maxBytes=1024*1024, backupCount=5)
file_handler = RotatingFileHandler("%s/%s" % (LOG_PATH, LOG_FILE), maxBytes=1024*1024, backupCount=5)
# 可以通过setFormatter指定输出格式
file_handler.setFormatter(formatter)
g_logger.addHandler(file_handler)
# import qdarkstyle
# from qdarkstyle.light.palette import LightPalette
g_xls_file = ''
g_template_file = ''
g_spirit_path = ''#os.getcwd()
g_font_path = ''#os.getcwd()
g_output_path = ''#os.getcwd()
g_spirit_list = []
g_font_list = []
g_offset_list = []
g_color_list = []
# g_times_list = []
g_text_list = []
g_layer_list = []
g_serial_list = []
g_error_sn_list = []
g_font_exd = ['.ttf','.otf','.FON','.PFM']
g_template_size = (0, 0)
g_font_size = 5
block_border = 95
img_to_text = 48
# min_img_to_text = 2
g_extend_name = '.tif'
g_mode = ColorMode.CMYK
class MyInputDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("软件注册")
style_file = os.getcwd() + "\\QSSFile" + "\\Ubuntu.qss"
qss_style = CommonHelper.readQss(style_file)
self.setStyleSheet(qss_style)
self.setWindowIcon(QIcon(os.getcwd() + "/images/icon.ico"))
self.setGeometry(200, 200, 700, 400)
# 创建并设置布局
layout = QVBoxLayout()
self.setLayout(layout)
# 添加文本输入框和确认按钮
self.label1 = QLabel('输入注册码')
# self.label1.setGeometry(QtCore.QRect(180, 70, 241, 51))
self.label1.setFixedSize(241, 51)
self.label1.setStyleSheet("QLabel{font-name: 黑体;font-size:26px; text-align: center; }")
self.label1.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.label1,0,Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignBottom)
self.line_edit = QLineEdit()
self.line_edit.setStyleSheet('border: 1px solid gray;font-size:18px;')
self.line_edit.setFixedSize(300, 50)
self.line_edit.setPlaceholderText('请输入')
self.line_edit.setEchoMode(QLineEdit.EchoMode.Password)
layout.addWidget(self.line_edit,0,Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignCenter)
self.button = QPushButton("确定")
self.button.clicked.connect(self.accept) # 连接按钮点击信号到对话框的 accept 槽函数
self.button.setFixedSize(200, 50)
self.button.setStyleSheet("background-color: #2CBFF8;font-size:18px;")
layout.addWidget(self.button,0,Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignTop)
self.center()
def show_dialog(self):
# 显示对话框并等待用户操作
return self.exec()
def get_text(self):
# 返回用户输入的文本
return self.line_edit.text()
def text_value(self):
# 返回 QLineEdit 中的文本
return self.line_edit.text()
def center(self):
screen = QtGui.QGuiApplication.primaryScreen().availableGeometry()
size = self.geometry()
self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))
class CommonHelper:
def __init__(self):
pass
@staticmethod
def readQss(style):
with open(style, 'r') as f:
return f.read()
class Worker(QThread):
update_progress_bar = pyqtSignal(int)
report_error = pyqtSignal(str)
report_sucess = pyqtSignal(str)
def __init__(self,total):
super().__init__()
self.total = total
self.running = True
def my_font_size(self,text,font):
if '\n' in text:
s0 = text.split('\n')[0]
s1 = text.split('\n')[1]
t_temp = s0 if len(s0) > len(s1) else s1
fb = font.getbbox(t_temp)
list1 = list(fb)
list1[3] = list1[1] + (list1[3] - list1[1]) * 2
r_box = tuple(list1)
return r_box
else:
fb = font.getbbox(text)
return fb
def get_font_size(self,fix_width, fix_height, text, fontsize, font_path):
#获取给定长宽区域的文字最合适字体大小
font = ImageFont.truetype(font_path, fontsize)
# 输入photo [height, width, channel]
jumpsize = 30
if '\n' in text:
t1, t2 = text.split('\n')[0],text.split('\n')[1]
text = t1 if len(t1) > len(t2) else t2
fix_height = fix_height // 2
while True:
# font.getsize() 可以获得当前文本所占图片大小,维度是[width, height]
# 用二分查找的方式搜索在给定比例下的最合适的size
s = font.getbbox(text)
if (((s[2] - s[0]) < fix_width) and ((s[3]-s[1])<fix_height)):
fontsize += jumpsize
else:
jumpsize = jumpsize // 2
fontsize -= jumpsize
font = ImageFont.truetype(font_path, fontsize)
if jumpsize <= 1:
break
if '\n' in text:
r_box = font.getbbox(text)
list1 = list(r_box)
list1[3] = list1[1] + (list1[3] -list1[1]) * 2
r_box = tuple(list1)
return fontsize, r_box
else:
r_box = font.getbbox(text)
return fontsize, r_box
def find_font_file(self,f_half_path):
global g_font_exd
for el in g_font_exd:
if os.path.exists(f_half_path+el):
return f_half_path+el
else:
return False
def clear_folder(self, folder_path):
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path):
os.remove(file_path)
else:
self.clear_folder(file_path)
os.rmdir(file_path)
def run(self):
alpha = 0
global g_template_size,g_color_list,g_font_list,g_text_list,g_spirit_list,g_offset_list,g_serial_list,g_font_size
# img = Image.new("RGBA", g_template_size, (0, 0, 0, alpha))
# d = ImageDraw.Draw(img)
i = 0
p = 0
one = len(g_layer_list)
# self.clear_folder(os.getcwd() + "\\png\\")
wi = WI(filename=os.getcwd()+'\\png\\temp.png')
while i < (len(g_spirit_list)):
buffer = io.BytesIO()
# wi = WI(filename='./temp.png')
try: