-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.py
570 lines (494 loc) · 25.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import sys
from PyQt6.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene,QWidget,QHBoxLayout,QVBoxLayout,QLabel,
QGraphicsPixmapItem, QGraphicsRectItem, QMainWindow,QMessageBox,
QGraphicsTextItem, QPushButton,QInputDialog,QComboBox,
QSlider,QVBoxLayout,QFileDialog,QColorDialog,QTextEdit,QDialog,QLineEdit)
from PyQt6.QtCore import Qt, QRectF
from PyQt6.QtGui import (QGuiApplication,QPixmap,
QPainter, QImage, QFont,
QKeySequence,QShortcut,QFont,
QFontDatabase,QColor,QIcon)
class ImageCreator(QDialog):
def __init__(self):
super().__init__()
self.initUI()
self.initLayout()
self.center()
def initUI(self):
self.setWindowTitle('输出一个纯色背景图片')
self.setGeometry(100,100,300,300)
self.setWindowIcon(QIcon('logo.png'))
def center(self):
qr=self.frameGeometry()
cp=QGuiApplication.primaryScreen().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def initLayout(self):
self.color_label = QLabel('当前颜色:(255,255,255,255)', self)
self.color_button = QPushButton('选择颜色', self)
self.color=QColor('black')
self.color_button.clicked.connect(self.showColorDialog)
color_layout=QHBoxLayout()
color_layout.addStretch(1)
color_layout.addWidget(self.color_label)
color_layout.addWidget(self.color_button)
color_layout.addStretch(1)
self.bg_a=QLabel('图片不透明度:255')
self.bg_a_slider=QSlider(Qt.Orientation.Horizontal)
self.bg_a_slider.setRange(0, 255)
self.bg_a_slider.setSingleStep(1)
self.bg_a_slider.setValue(255)
bg_a_layout=QHBoxLayout()
bg_a_layout.addStretch(1)
bg_a_layout.addWidget(self.bg_a)
bg_a_layout.addWidget(self.bg_a_slider)
bg_a_layout.addStretch(1)
self.bg_a_slider.valueChanged.connect(self.update_bg_a_label)
self.color_pre=QLabel('颜色预览:')
self.color_rect=QLabel()
self.color_rect.setFixedSize(50,50)
self.color_rect.setStyleSheet(f'background-color: {self.color.name()}')
color_pre_layout=QHBoxLayout()
color_pre_layout.addStretch(1)
color_pre_layout.addWidget(self.color_pre)
color_pre_layout.addWidget(self.color_rect)
color_pre_layout.addStretch(1)
self.width_label = QLabel('图片长度:', self)
self.width_input_line = QLineEdit(self)
self.width_input_line.setPlaceholderText('在这里输入整数')
self.width_unit_label = QLabel('像素', self)
width_input_layout = QHBoxLayout()
width_input_layout.addStretch(1)
width_input_layout.addWidget(self.width_label)
width_input_layout.addWidget(self.width_input_line)
width_input_layout.addWidget(self.width_unit_label)
width_input_layout.addStretch(1)
self.height_label = QLabel('图片宽度:', self)
self.height_input_line = QLineEdit(self)
self.height_input_line.setPlaceholderText('在这里输入整数')
self.height_unit_label = QLabel('像素', self)
height_input_layout = QHBoxLayout()
height_input_layout.addStretch(1)
height_input_layout.addWidget(self.height_label)
height_input_layout.addWidget(self.height_input_line)
height_input_layout.addWidget(self.height_unit_label)
height_input_layout.addStretch(1)
self.create_button = QPushButton('创建图片', self)
self.create_button.clicked.connect(self.createImage)
layout = QVBoxLayout()
layout.addLayout(color_layout)
layout.addLayout(bg_a_layout)
layout.addLayout(color_pre_layout)
layout.addLayout(width_input_layout)
layout.addLayout(height_input_layout)
layout.addWidget(self.create_button)
self.setLayout(layout)
def update_bg_a_label(self):
self.bg_a.setText(f'图片不透明度:{self.bg_a_slider.value()}')
self.color.setAlpha(self.bg_a_slider.value())
self.color_rect.setStyleSheet(f'background-color: {self.color.name()}')
def showColorDialog(self):
color = QColorDialog.getColor()
if color.isValid():
self.color_label.setText(f'当前颜色:{self.color.getRgb()}')
self.color_button.setStyleSheet(f'background-color: {color.name()}')
self.color_rect.setStyleSheet(f'background-color: {color.name()}')
self.color = color
def createImage(self):
width = int(self.width_input_line.text())
height = int(self.height_input_line.text())
if width <= 0 or height <= 0:
return
if not hasattr(self, 'color'):
return
image = QPixmap(width, height)
image.fill(self.color)
image.save(f'./bgs/{self.color.name()}-{width}x{height}.png')
QMessageBox.information(self, '提示', '图片已保存在 bgs 目录下!', QMessageBox.StandardButton.Ok)
# print(f'图片已创建: {self.color.name()}-{width}x{height}.png')
class HandFontWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 1200, 600)
self.setWindowTitle('手写字体图片生成器')
self.setWindowIcon(QIcon('logo.png'))
self.initView()
self.config_widget=self.initConfigForm()
# 将配置窗口添加到主窗口的布局中
main_layout = QHBoxLayout()
main_layout.addWidget(self.config_widget)
main_layout.addWidget(self.view)
main_widget = QWidget(self)
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
# 添加导出图片的快捷键 Ctrl + S
export_shortcut = QShortcut(QKeySequence(QKeySequence.StandardKey.Save), self)
export_shortcut.activated.connect(self.view.exportSceneToImage)
self.updateView()
self.show()
def initView(self):
self.my_scene = QGraphicsScene(self)
self.view = GraphicsView(self.my_scene, self)
self.setCentralWidget(self.view)
def initConfigForm(self):
# 创建配置参数窗口
config_widget = QWidget(self)
config_layout = QVBoxLayout(config_widget)
# 添加文字区域配置
text_config_label = QLabel('效果配置:')
config_layout.addWidget(text_config_label)
# 选择背景图片
self.label_bg=QLabel('letter.png')
self.button_bg=QPushButton('选择背景图')
self.button_bg.clicked.connect(self.show_file_dialog)
self.bg_path='./bgs/letter.png'
config_layout.addWidget(self.label_bg)
config_layout.addWidget(self.button_bg)
creat_bg_layout=QHBoxLayout()
creat_bg_label=QLabel('没有喜欢的背景图片?')
creat_bg_button=QPushButton('创建一个纯色背景')
creat_bg_button.clicked.connect(self.create_bg_widget)
creat_bg_layout.addWidget(creat_bg_label)
creat_bg_layout.addWidget(creat_bg_button)
config_layout.addLayout(creat_bg_layout)
# 选择字体
self.label_font = QLabel("选择字体:")
self.combo_box = QComboBox(self)
self.combo_box.addItem('hand.ttf')
self.combo_box.addItem('李国夫董事长手写体.ttf')
self.combo_box.addItem('品如手写体.ttf')
self.combo_box.addItem('青叶手写体.ttf')
self.combo_box.addItem('上首鸿志手写体.ttf')
self.combo_box.addItem('手写大象体.ttf')
self.combo_box.addItem('未知手写体_1.ttf')
self.combo_box.addItem('薛文轩钢笔楷体.ttf')
self.combo_box.addItem('张维镜手写楷书.ttf')
config_layout.addWidget(self.label_font)
config_layout.addWidget(self.combo_box)
self.combo_box.currentIndexChanged.connect(self.updateView)
# 选择字体颜色
self.label_font_color = QLabel("当前字体颜色:black")
self.button_font_color = QPushButton('选择字体颜色')
self.font_color='black'
config_layout.addWidget(self.label_font_color)
config_layout.addWidget(self.button_font_color)
self.button_font_color.clicked.connect(self.show_color_dialog)
self.label_text=QLabel('要显示的文字:')
self.text_edit = QTextEdit(self)
self.text_edit.setPlaceholderText('要显示的文字')
self.text_edit.setText('''花费了一天,跟gpt3.5对话进百句,修修改改,最终做成这样的效果。
我的字迹一直很差,我也很讨厌写字,我花费了很多时间在练字上但我的字迹不好反坏。所以当我看到生成手写字图片的可能时,我便开始了迫不及待地尝试。
做到一半的时候我突然想到网上有没有已经做出来的。确实有,有点小失望,但又细看了一下发现它的应该操作比我的复杂点,所以我又继续做完就是现在的效果。''')
# text_edit.toPlainText()
self.text_edit.textChanged.connect(self.updateView)
config_layout.addWidget(self.label_text)
config_layout.addWidget(self.text_edit)
# 坐标信息
# 创建滑块调节数值并为其设置标签显示当前数值
self.slider_x = QSlider(Qt.Orientation.Horizontal)
self.slider_x.setRange(0, 500)
self.slider_x.setSingleStep(1)
self.slider_x.valueChanged.connect(self.updateView)
self.slider_x.setValue(0)
self.label_x = QLabel("文字坐标x:0")
config_layout.addWidget(self.label_x)
config_layout.addWidget(self.slider_x)
self.slider_y = QSlider(Qt.Orientation.Horizontal)
self.slider_y.setRange(0, 800)
self.slider_y.setSingleStep(1)
self.slider_y.valueChanged.connect(self.updateView)
self.slider_x.setValue(0)
self.label_y = QLabel("文字坐标y:0")
config_layout.addWidget(self.label_y)
config_layout.addWidget(self.slider_y)
# 文字区域宽度
self.slider_width = QSlider(Qt.Orientation.Horizontal)
self.slider_width.setRange(10, 2000)
self.slider_width.setSingleStep(1)
self.slider_width.setValue(100)
self.slider_width.valueChanged.connect(self.updateView)
self.label_width = QLabel("文字显示宽度:100")
config_layout.addWidget(self.label_width)
config_layout.addWidget(self.slider_width)
self.slider_height = QSlider(Qt.Orientation.Horizontal)
self.slider_height.setRange(1, 2000)
self.slider_height.setSingleStep(1)
self.slider_height.setValue(200)
self.slider_height.valueChanged.connect(self.updateView)
self.label_height = QLabel("文字显示高度:200")
config_layout.addWidget(self.label_height)
config_layout.addWidget(self.slider_height)
# 字体大小
self.slider_font_size = QSlider(Qt.Orientation.Horizontal)
self.slider_font_size.setRange(1, 100)
self.slider_font_size.setSingleStep(1)
self.slider_font_size.setValue(16)
self.slider_font_size.valueChanged.connect(self.updateView)
self.label_font_size = QLabel("文字大小:16")
config_layout.addWidget(self.label_font_size)
config_layout.addWidget(self.slider_font_size)
# 字体粗细
self.slider_font_weight = QSlider(Qt.Orientation.Horizontal)
self.slider_font_weight.setRange(100, 1000)
self.slider_font_weight.setSingleStep(100)
self.slider_font_weight.setValue(500)
self.slider_font_weight.valueChanged.connect(self.updateView)
self.label_font_weight = QLabel("文字粗细:500")
config_layout.addWidget(self.label_font_weight)
config_layout.addWidget(self.slider_font_weight)
# 字体间距
self.slider_font_spacing = QSlider(Qt.Orientation.Horizontal)
self.slider_font_spacing.setRange(-20,20)
self.slider_font_spacing.setSingleStep(1)
self.slider_font_spacing.setValue(2)
self.slider_font_spacing.valueChanged.connect(self.updateView)
self.label_font_spacing = QLabel("字体间距:0.1")
config_layout.addWidget(self.label_font_spacing)
config_layout.addWidget(self.slider_font_spacing)
# 行间距
self.slider_line_spacing = QSlider(Qt.Orientation.Horizontal)
self.slider_line_spacing.setRange(20,80)
self.slider_line_spacing.setSingleStep(1)
self.slider_line_spacing.setValue(40)
self.slider_line_spacing.valueChanged.connect(self.updateView)
self.label_line_spacing = QLabel("行间距:1")
config_layout.addWidget(self.label_line_spacing)
config_layout.addWidget(self.slider_line_spacing)
# 添加导出按钮
export_btn = QPushButton('导出图片:', self)
export_btn.clicked.connect(self.view.exportSceneToImage)
config_layout.addWidget(export_btn)
config_widget.setLayout(config_layout)
return config_widget
def create_bg_widget(self):
self.dialog=ImageCreator()
self.dialog.show()
def show_file_dialog(self):
# 创建文件选择框
file_dialog = QFileDialog(self)
file_dialog.setWindowTitle('Open File')
file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
# 显示文件选择框并等待用户选择
if file_dialog.exec() == QFileDialog.DialogCode.Accepted:
# 获取用户选择的文件路径
selected_file = file_dialog.selectedFiles()[0]
self.label_bg.setText(selected_file)
self.bg_path=selected_file
self.updateView()
def show_color_dialog(self):
# 创建颜色选择框
color_dialog = QColorDialog(self)
color_dialog.setWindowTitle('Choose Color')
# 设置初始颜色(可选)
color_dialog.setCurrentColor(QColor(255, 0, 0))
# 显示颜色选择框并等待用户选择
if color_dialog.exec() == QColorDialog.DialogCode.Accepted:
# 获取用户选择的颜色
self.font_color = color_dialog.currentColor().name()
self.label_font_color.setText('当前字体颜色:'+self.font_color)
self.updateView()
def updateView(self):
current_text = self.combo_box.currentText()
font_path='./fonts/'+current_text
self.label_x.setText(f"文字坐标x:{self.slider_x.value()}")
self.label_y.setText(f"文字坐标y:{self.slider_y.value()}")
self.label_font_size.setText(f"文字大小:{self.slider_font_size.value()}")
self.label_font_weight.setText(f"文字粗细:{self.slider_font_weight.value()}")
self.label_font_color.setText(f"当前字体颜色:{self.font_color}")
self.label_font_spacing.setText(f"文字间隔:{round(self.slider_font_spacing.value()/20,2)}")
self.label_line_spacing.setText(f"行间隔x:{round(self.slider_line_spacing.value()/20,2)}")
self.label_width.setText(f"文字显示宽度:{self.slider_width.value()}")
self.label_height.setText(f"文字显示高度:{self.slider_height.value()}")
text_tmp=self.text_edit.toPlainText().replace(' ',' ').replace('\n','<br>')
self.view.addTextItem(text=text_tmp,
bg_path=self.bg_path,
font_path=font_path,
font_color=self.font_color,
font_weight=self.slider_font_weight.value(),
x=self.slider_x.value(),y=self.slider_y.value(),
font_size=self.slider_font_size.value(),
font_spacing=round(self.slider_font_spacing.value()/20,2),
line_spacing=round(self.slider_line_spacing.value()/40,2),
rect_width=self.slider_width.value(),
rect_height=self.slider_height.value()
)
class GraphicsView(QGraphicsView):
def __init__(self, scene, parent):
super().__init__(scene, parent)
self.setStyleSheet('GraphicsView{background-color:#FDF6E3;}')
self.setRenderHint(QPainter.RenderHint.Antialiasing, True)
self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag) # 启用拖拽模式
self.drawing_rect = None
self.origin = None
self.is_drawing = False
self.coordinate_text_items = {} # 存储坐标信息的字典,以矩形为键
def wheelEvent(self, event):
modifiers = event.modifiers()
if modifiers == Qt.KeyboardModifier.ControlModifier:
# 只有在按下 Ctrl 键时才缩放
factor = 1.2 # 缩放因子
if event.angleDelta().y() < 0:
factor = 1.0 / factor # 对于负的滚轮事件进行缩小
self.scale(factor, factor)
def mouseDoubleClickEvent(self, event):
super().mouseDoubleClickEvent(event)
if event.button() == Qt.MouseButton.LeftButton:
# 获取鼠标双击的位置
double_click_position = event.pos()
# 获取双击位置相对于原图的像素位置
double_click_scene_position = self.mapToScene(double_click_position)
# 获取点击位置相对于原图的像素位置
item = self.scene().itemAt(double_click_scene_position, self.transform())
if isinstance(item, QGraphicsRectItem):
# 删除被点击的矩形的坐标信息
rect_key = id(item)
if rect_key in self.coordinate_text_items:
self.removeCoordinateTextItems(self.coordinate_text_items[rect_key])
# 删除被点击的矩形
self.scene().removeItem(item)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if event.buttons() & Qt.MouseButton.RightButton:
if self.is_drawing and self.origin is not None:
# 获取鼠标当前位置
current_position = event.pos()
# 获取当前位置相对于原图的像素位置
current_scene_position = self.mapToScene(current_position)
# 更新矩形的大小
self.drawing_rect.setRect(QRectF(self.origin, current_scene_position))
# 实时显示图像
scene_rect = self.sceneRect()
self.setSceneRect(scene_rect)
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
if event.button() == Qt.MouseButton.RightButton:
if self.is_drawing and self.origin is not None:
self.is_drawing = False
# 获取鼠标释放的位置
release_position = event.pos()
# 获取释放位置相对于原图的像素位置
release_scene_position = self.mapToScene(release_position)
# 更新矩形的大小
self.drawing_rect.setRect(QRectF(self.origin, release_scene_position))
# 显示矩形的坐标信息
self.showRectCoordinates(self.drawing_rect)
def mousePressEvent(self, event):
super().mousePressEvent(event)
if event.button() == Qt.MouseButton.RightButton:
if not self.is_drawing:
# 获取鼠标点击的位置
click_position = event.pos()
# 获取点击位置相对于原图的像素位置
scene_position = self.mapToScene(click_position)
self.origin = scene_position
self.is_drawing = True
# 创建一个新的矩形
self.drawing_rect = QGraphicsRectItem(QRectF(self.origin, self.origin))
self.scene().addItem(self.drawing_rect)
def showRectCoordinates(self, rect_item):
# 获取矩形的左上角和右下角相对于图片的坐标
rect_top_left = rect_item.rect().topLeft()
rect_bottom_right = rect_item.rect().bottomRight()
# 将坐标信息显示在矩形的左上角和右下角
# 在矩形的左上角显示坐标信息
text_item_top_left = QGraphicsTextItem(f"({int(rect_top_left.x())}, {int(rect_top_left.y())})")
text_item_top_left.setFont(QFont('Arial', 10))
text_item_top_left.setDefaultTextColor(Qt.GlobalColor.red)
text_item_top_left.setPos(rect_top_left.x() + 5, rect_top_left.y() - 20)
self.scene().addItem(text_item_top_left)
# 在矩形的右下角显示坐标信息
text_item_bottom_right = QGraphicsTextItem(f"({int(rect_bottom_right.x())}, {int(rect_bottom_right.y())})")
text_item_bottom_right.setFont(QFont('Arial', 10))
text_item_bottom_right.setDefaultTextColor(Qt.GlobalColor.red)
text_item_bottom_right.setPos(rect_bottom_right.x() - 50, rect_bottom_right.y() + 5)
self.scene().addItem(text_item_bottom_right)
# 将矩形对象作为键,将坐标信息文本项存储到字典中
rect_key = id(rect_item)
self.coordinate_text_items[rect_key] = [text_item_top_left, text_item_bottom_right]
def removeCoordinateTextItems(self, text_items):
# 从场景中删除坐标信息文本项
for text_item in text_items:
self.scene().removeItem(text_item)
# 清空字典中的坐标信息
text_items.clear()
def addTextItem(self, text,
bg_path='./bgs/letter.png',
font_path="./fonts/hand.ttf",font_size=12,
font_color='black',
font_weight=600,
x=100,y=100,
font_spacing=0, line_spacing=1.2,
rect_width=500, rect_height=50):
# 清除先前的文本项
self.scene().clear()
#加载背景图片
pixmap = QPixmap(bg_path)
item = QGraphicsPixmapItem(pixmap)
self.scene().addItem(item)
self.scene().setSceneRect(0, 0, 5000, 5000)
# 创建一个QGraphicsTextItem
text_item = QGraphicsTextItem()
text_item.setPos(x, y)
# 加载本地字体文件,设置字体大小
font_id = QFontDatabase.addApplicationFont(font_path)
font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
font = QFont(font_family,font_size)
text_item.setFont(font)
# 使用 HTML 样式设置文本,包括换行和调整行间距、文字间距
html_text = f"<div style='font-weight: {font_weight};color:{font_color};font-size:{font_size}pt; line-height:{line_spacing * 100}%; letter-spacing:{font_spacing}em;'>{text}</div>"
text_item.setHtml(html_text)
# 设置文本项的边界矩形
text_item.setTextInteractionFlags(Qt.TextInteractionFlag.TextEditorInteraction)
text_item.setTextWidth(rect_width)
# 将文本项添加到场景中
self.scene().addItem(text_item)
def exportSceneToImage(self):
# 获取场景的范围
scene_rect = self.sceneRect()
# 获取场景内容的大小
content_rect = self.scene().itemsBoundingRect()
# 使用场景内容的大小来确定导出图片的大小
image_size = content_rect.size().toSize()
# 弹出输入对话框获取用户输入的文件名,默认为"img_1.png"
file_name, _ = QInputDialog.getText(self, '导出图片', '输入文件名', text='img_1.png')
# 如果用户取消了输入,则返回
if not file_name:
return
# 创建一个 QImage 对象,使用 ARGB32_Premultiplied 格式,支持透明度
image = QImage(image_size, QImage.Format.Format_ARGB32_Premultiplied)
image.fill(Qt.GlobalColor.transparent) # 使用透明背景
# 创建一个QPainter对象
painter = QPainter(image)
# 将场景渲染到QImage中,以内容的大小为主
self.scene().render(painter, QRectF(image.rect()), content_rect)
# 结束绘制
painter.end()
# 保存QImage为图片文件
image.save(file_name)
QMessageBox.information(self, '提示', '图片已保存在当前目录下!', QMessageBox.StandardButton.Ok)
# def exportSceneToImage(self):
# # 获取场景的范围
# scene_rect = self.sceneRect()
# # 弹出输入对话框获取用户输入的文件名,默认为"img_1.png"
# file_name, _ = QInputDialog.getText(self, '导出图片', '输入文件名', text='img_1.png')
# # 如果用户取消了输入,则返回
# if not file_name:
# return
# # 创建一个QImage对象
# image = QImage(scene_rect.size().toSize(), QImage.Format.Format_ARGB32)
# image.fill(Qt.GlobalColor.white)
# # 创建一个QPainter对象
# painter = QPainter(image)
# # 将场景渲染到QImage中
# self.scene().render(painter, QRectF(image.rect()), scene_rect)
# # 结束绘制
# painter.end()
# # 保存QImage为图片文件
# image.save(file_name)
# QMessageBox.information(self, '提示', '图片已保存在当前目录下!', QMessageBox.StandardButton.Ok)
if __name__ == '__main__':
app = QApplication(sys.argv)
viewer = HandFontWindow()
sys.exit(app.exec())