写了个小工具,随手贴出来
批量生成,文字到背景图片
import sys
import os
import time
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QTextEdit, QPushButton, QColorDialog, QFileDialog, QMessageBox
from PyQt5.QtGui import QPixmap
from PIL import Image, ImageDraw, ImageFont
def draw\_text(text, font\_size=200, font\_stroke=0, color="red", font='DOUYUFONT.OTF',
background\_image="bg.jpg",
border=20, border\_color="white",
max\_fill=60, raw\_y=0, save\_name="output\_image.jpg",):
background\_image = Image.open(background\_image)
font = ImageFont.truetype(font, font\_size)
text\_width, text\_height = font.getsize(text)
max\_text\_width = int(background\_image.width \* max\_fill / 100)
wrapped\_text = text
wrapped\_lines = \[\]
if "##" in text:
wrapped\_lines = text.split("##")
maxlen = len(wrapped\_lines\[0\])
for i in wrapped\_lines\[1:\]:
leni = len(i)
if leni > maxlen:
maxlen = leni
for i, v in enumerate(wrapped\_lines):
wrapped\_lines\[i\] = " "\*(maxlen-len(v))\*3 + v
elif text\_width > max\_text\_width:
line = ""
for word in text:
if font.getsize(line + word)\[0\] <= max\_text\_width:
line += word
else:
wrapped\_lines.append(line)
line = word
if line:
wrapped\_lines.append(line)
if len(wrapped\_lines) > 0:
wrapped\_text = "\\n".join(wrapped\_lines)
text\_width, text\_height = font.getsize\_multiline(wrapped\_text)
x = (background\_image.width - text\_width) // 2
y = (background\_image.height - text\_height) // 2 + raw\_y
draw = ImageDraw.Draw(background\_image)
if font\_stroke:
draw.text((x-font\_stroke, y), wrapped\_text, fill=color, font=font, stroke\_width=border, stroke\_fill=border\_color)
draw.text((x+font\_stroke, y), wrapped\_text, fill=color, font=font, stroke\_width=border, stroke\_fill=border\_color)
draw.text((x, y-font\_stroke), wrapped\_text, fill=color, font=font, stroke\_width=border, stroke\_fill=border\_color)
draw.text((x, y+font\_stroke), wrapped\_text, fill=color, font=font, stroke\_width=border, stroke\_fill=border\_color)
draw.text((x, y), wrapped\_text, fill=color, font=font)
else:
draw.text((x, y), wrapped\_text, fill=color, font=font, stroke\_width=border, stroke\_fill=border\_color)
try:
background\_image.save(save\_name)
except:
background\_image.save("result\_%d\_.jpg" % time.time())
class Dialog(QWidget):
color\_board = '#ffffff'
color\_font = '#3b2dff'
font = "default.otf"
background = "bg.jpg"
def \_\_init\_\_(self):
super().\_\_init\_\_()
self.setWindowTitle('文字图片批量生成 by GKK')
\# 创建布局
main\_layout = QHBoxLayout()
\# 左侧布局
left\_layout = QVBoxLayout()
self.text\_input = QTextEdit()
self.text\_input.setPlaceholderText("\\n\\n\\n多张图片时使用 回车换行,\\n单张文字中输入## 替换 换行,\\n文本默认自动换行\\n字体默认使用:default.otf\\n背景图片默认使用:bg.jpg\\n")
self.text\_input.setMinimumWidth(400)
left\_layout.addWidget(self.text\_input)
\# 右侧布局
right\_layout = QVBoxLayout()
\# 背景图片选择框
self.background\_button = QPushButton('选择背景图片')
self.background\_button.clicked.connect(self.choose\_background)
right\_layout.addWidget(self.background\_button)
self.background\_label = QLabel()
self.background\_label.setPixmap(QPixmap(self.background).scaledToWidth(220))
right\_layout.addWidget(self.background\_label)
\# 字体选择框
self.font\_button = QPushButton('选择字体')
self.font\_button.setText(os.path.basename(self.font))
self.font\_button.clicked.connect(self.choose\_font)
right\_layout.addWidget(self.font\_button)
\# 字体大小
self.number1\_input = QLineEdit()
self.number1\_input.setText("250")
number1\_layout = QHBoxLayout()
number1\_layout.addWidget(QLabel('字体大小'))
number1\_layout.addWidget(self.number1\_input)
right\_layout.addLayout(number1\_layout)
\# 字体颜色
self.color1\_button = QPushButton('字体颜色')
self.color1\_button.clicked.connect(self.choose\_color1)
self.color1\_label = QLabel('')
self.color1\_label.setStyleSheet(f'background-color: {self.color\_font};')
color1\_layout = QHBoxLayout()
color1\_layout.addWidget(self.color1\_button)
color1\_layout.addWidget(self.color1\_label)
right\_layout.addLayout(color1\_layout)
self.number5\_input = QLineEdit()
self.number5\_input.setText("0")
number5\_layout = QHBoxLayout()
number5\_layout.addWidget(QLabel('字体加粗'))
number5\_layout.addWidget(self.number5\_input)
\# right\_layout.addLayout(number5\_layout)
\# 边框大小
self.number2\_input = QLineEdit()
self.number2\_input.setText("30")
number2\_layout = QHBoxLayout()
number2\_layout.addWidget(QLabel('边框大小'))
number2\_layout.addWidget(self.number2\_input)
right\_layout.addLayout(number2\_layout)
\# 边框颜色
self.color2\_button = QPushButton('边框颜色')
self.color2\_button.clicked.connect(self.choose\_color2)
self.color2\_label = QLabel('')
self.color2\_label.setStyleSheet(f'background-color: {self.color\_board};')
color2\_layout = QHBoxLayout()
color2\_layout.addWidget(self.color2\_button)
color2\_layout.addWidget(self.color2\_label)
right\_layout.addLayout(color2\_layout)
\# 占比
self.number3\_input = QLineEdit()
self.number3\_input.setText("80")
number3\_layout = QHBoxLayout()
number3\_layout.addWidget(QLabel('自动换行宽度百分比'))
number3\_layout.addWidget(self.number3\_input)
right\_layout.addLayout(number3\_layout)
self.number4\_input = QLineEdit()
self.number4\_input.setText("-10")
number4\_layout = QHBoxLayout()
number4\_layout.addWidget(QLabel('文字上下偏移(向上偏填负值)'))
number4\_layout.addWidget(self.number4\_input)
right\_layout.addLayout(number4\_layout)
\# 开始
self.start\_button = QPushButton('开始生成')
self.start\_button.clicked.connect(self.start)
right\_layout.addWidget(self.start\_button)
\# self.start\_label = QLabel('')
\# start\_layout = QHBoxLayout()
\# start\_layout.addWidget(self.start\_button)
\# start\_layout.addWidget(self.start\_label)
\# right\_layout.addLayout(start\_layout)
\# 添加右侧布局到主布局
main\_layout.addLayout(left\_layout)
main\_layout.addLayout(right\_layout)
\# 创建文本输出框
self.text\_output = QTextEdit()
self.text\_output.setMaximumHeight(200)
self.text\_output.setReadOnly(True)
\# 添加主布局和文本输出框到窗口
layout = QVBoxLayout()
layout.addLayout(main\_layout)
\# layout.addWidget(self.text\_output)
self.setLayout(layout)
def start(self):
if self.background:
try:
for i, text in enumerate(self.text\_input.toPlainText().splitlines()):
draw\_text(text,
int(self.number1\_input.text()),
int(self.number5\_input.text()),
self.color\_font,
self.font,
self.background,
int(self.number2\_input.text()),
self.color\_board,
int(self.number3\_input.text()),
int(self.number4\_input.text()),
"r\_"+text.replace(" ", "").replace(".", "").replace("##", "")+".jpg"
)
QMessageBox.about(self, "帮助", '生成完成,总数:%d ' % (i+1))
except Exception as e:
QMessageBox.warning(self, '警告', e)
def choose\_font(self):
self.font, \_ = QFileDialog.getOpenFileName(self, '选择字体', '', 'Font files (\*.ttf \*.otf)')
self.font\_button.setText(os.path.basename(self.font))
def choose\_background(self):
self.background, \_ = QFileDialog.getOpenFileName(self, '选择背景图片', '', 'Image files (\*.png \*.jpg \*.bmp)')
self.background\_label.clear()
self.background\_label.setPixmap(QPixmap(self.background).scaledToHeight(100))
def choose\_color1(self):
color = QColorDialog.getColor()
if color.isValid():
self.color\_font = color.name()
self.color1\_label.setStyleSheet(f'background-color: {self.color\_font};')
def choose\_color2(self):
color = QColorDialog.getColor()
if color.isValid():
self.color\_board = color.name()
self.color2\_label.setStyleSheet(f'background-color: {self.color\_board};')
if \_\_name\_\_ == '\_\_main\_\_':
app = QApplication(sys.argv)
dialog = Dialog()
dialog.show()
sys.exit(app.exec\_())
分享主题:
工具/资源