-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextConverter.py
294 lines (245 loc) · 11.7 KB
/
TextConverter.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
from SelectionMenu import TextDefMenu
from ImageMenu import ImgDefMenu
from LayerMenu import LayerMenu
from PyQt6.QtWidgets import QDialog, QGraphicsView, QGraphicsScene, QVBoxLayout, QGraphicsPixmapItem, QFileDialog, QSizePolicy, QWidget, QPushButton, QHBoxLayout, QSplitter, QListWidget, QMessageBox
from PyQt6.QtGui import QPixmap, QIcon, QKeyEvent, QPainter
from PyQt6.QtCore import Qt
from PIL import Image, ImageDraw, ImageFont
import sys
import os
import textwrap
class TextConverterMenu(QDialog):
def __init__(self, image_path, parent=None):
super().__init__(parent)
self.image_path = image_path
# Set up window
self.window_width, self.window_height = 800, 500
self.setMinimumSize(self.window_width, self.window_height)
self.setWindowTitle("Autodesign")
self.setWindowIcon(QIcon("Resources/Icons/ACM_logo.jpg")) # Provide the path to your icon image
main_layout = QHBoxLayout(self)
splitter = QSplitter(Qt.Orientation.Horizontal) # Splitter to divide the window into two parts
# Create QGraphicsView and QGraphicsScene
self.graphicsView = QGraphicsView(self)
self.graphicsView.setRenderHint(QPainter.RenderHint.Antialiasing)
self.graphicsView.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
self.graphicsScene = QGraphicsScene(self)
self.graphicsView.setScene(self.graphicsScene)
self.graphicsView.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self.graphicsView.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.graphicsView.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.graphicsView.wheelEvent = self.zoom
main_layout.addWidget(self.graphicsView)
# Add QGraphicsView to the left side of the splitter
splitter.addWidget(self.graphicsView)
# Create a user menu on the right side
user_menu_layout = QVBoxLayout()
user_menu_widget = QWidget(self)
user_menu_widget.setLayout(user_menu_layout)
button1 = QPushButton("Add Text")
button1.pressed.connect(self.add3dText)
button2 = QPushButton("Add Image")
button2.pressed.connect(self.addOverlayImage)
button4 = QPushButton("Save Image")
button4.pressed.connect(self.saveFile)
self.layersListWidget = QListWidget()
self.layersListWidget.setDragEnabled(True)
self.layersListWidget.setDragDropMode(QListWidget.DragDropMode.InternalMove)
self.layersListWidget.model().rowsMoved.connect(self.updateLayerOrder)
self.layersListWidget.itemDoubleClicked.connect(self.onLayersItemClick)
# Add buttons to layout
user_menu_layout.addWidget(button1)
user_menu_layout.addWidget(button2)
user_menu_layout.addWidget(button4)
user_menu_layout.addWidget(self.layersListWidget)
splitter.addWidget(user_menu_widget)
splitter.setSizes([int(self.window_width * 0.75), int(self.window_width * 0.25)])
main_layout.addWidget(splitter)
self.layersList = []
# Load the image
self.pixmap = QPixmap(image_path)
self.item = QGraphicsPixmapItem(self.pixmap)
self.graphicsScene.addItem(self.item)
self.savePreview()
base_layer = ["isImg", self.image_path, 0, 0]
self.addLayer(base_layer, "Base Image")
self.maximized = False
self.show()
def updateLayerOrder(self):
# Create a new ordered list based on the QListWidget order
new_layers_list = []
print("Before: ", self.layersList)
for i in range(self.layersListWidget.count()):
item_name = self.layersListWidget.item(i).text()
for layer in self.layersList:
if layer[1] == item_name:
new_layers_list.append(layer)
break
self.layersList = new_layers_list
print("After: ", self.layersList)
self.updatePreview()
def zoom(self, event):
factor = 1.025
if event.angleDelta().y() < 0:
factor = 1.0 / factor
self.graphicsView.setTransform(self.graphicsView.transform().scale(factor, factor))
def add3dText(self):
self.setDisabled(True)
textDefMenu = TextDefMenu()
textDefMenu.exec()
font, text, fontsize, threeD, color, depth, x, y = textDefMenu.conclude()
layer_item = [font, text, fontsize, threeD, color, depth, x, y]
self.addLayer(layer_item, layer_item[1])
self.setDisabled(False)
def showImageEditWindow(self):
self.setDisabled(True)
imgDefMenu = ImgDefMenu()
imgDefMenu.exec()
layer_item = imgDefMenu.conclude()
self.addLayer(layer_item, "Image")
self.setDisabled(False)
def addOverlayImage(self):
file_filter = 'Image File (*.png *.jpg)'
response = QFileDialog.getOpenFileName(
parent = self,
caption = 'Select a file',
directory = os.getcwd(),
filter = file_filter,
initialFilter = 'Image File (*.png *.jpg)'
)
if response[0]: # Check if a valid file was selected
imageLayer = ["isImg", response[0], 0, 0]
self.addLayer(imageLayer, "Img")
self.drawOverlayImage(response[0], 0, 0)
else:
print("Import canceled or invalid file selected.")
def updatePreview(self):
print("current layerslist: ", self.layersList)
# Remove all items from the scene
for item in self.graphicsScene.items():
self.graphicsScene.removeItem(item)
self.drawLayers()
self.pixmap = QPixmap('output.png')
self.item.setPixmap(self.pixmap)
self.graphicsScene.addItem(self.item)
self.layersListWidget.clear()
for layer_item in self.layersList:
self.layersListWidget.addItem(layer_item[1]) # Only add Text to layers view
def onLayersItemClick(self):
self.setDisabled(True)
selected_index = self.layersListWidget.currentRow()
if (self.layersList[selected_index][0] == "isImg"):
layerEditor = ImgDefMenu()
print(self.layersList[selected_index])
layerEditor.loadLayer(self.layersList[selected_index])
layerEditor.exec()
subLayer = layerEditor.conclude() # Layer that will substitute the old one
self.layersList[selected_index] = subLayer
if (subLayer[1] == ''): # Empty layers should be ignored
self.layersList.remove(subLayer)
self.updatePreview()
self.setDisabled(False)
else:
layerEditor = LayerMenu()
layerEditor.loadLayer(self.layersList[selected_index])
layerEditor.exec()
subLayer = layerEditor.conclude() # Layer that will substitute the old one
self.layersList[selected_index] = subLayer
if (subLayer[1] == ''): # Empty layers should be ignored
self.layersList.remove(subLayer)
self.updatePreview()
self.setDisabled(False)
def deleteLayer(self, layerIndex):
self.layersList.pop(layerIndex)
self.updatePreview()
def drawLayers(self):
output_path = "output.png"
path = self.image_path
if (self.layersList == []):
with Image.open(path) as im:
im.save(output_path)
im.close()
for layer_item in self.layersList:
print("Layer item: ", layer_item)
if layer_item[0] == "isImg":
self.drawImage(layer_item)
else:
self.drawText(layer_item, path, output_path)
path = output_path
def drawText(self, layer_item, path, output_path):
font = layer_item[0]
text = layer_item[1]
fontsize = layer_item[2]
threeD = layer_item[3]
color = layer_item[4]
depth = layer_item[5]
x = layer_item[6]
y = layer_item[7]
wrapped_text = textwrap.fill(text, width=20) # Adjust the width as needed
with Image.open(path) as im:
fontPIL = ImageFont.truetype('./Custom fonts/'+ font, fontsize)
draw = ImageDraw.Draw(im)
text_position = (x, y) # Position of the text
if threeD == True:
for i in range(depth):
text_color = (i, i, i) # RGB color for the text
text_position = (x + i, y + i) # Position of the text
draw.text(text_position, wrapped_text, font=fontPIL, fill=text_color)
draw.text(text_position, wrapped_text, font=fontPIL, fill=color)
im.save(output_path)
im.close()
def drawImage(self, layer_item):
self.drawOverlayImage(layer_item[1], layer_item[2], layer_item[3])
self.savePreview()
def savePreview(self):
rect = self.graphicsScene.sceneRect() # Determine size of the scene's bounding rect
result_pixmap = QPixmap(int(rect.width()), int(rect.height()))
result_pixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(result_pixmap)
self.graphicsScene.render(painter, target=rect, source=rect) # Render the scene onto the QPixmap
painter.end()
result_pixmap.save('output.png')
print("Preview saved!\n")
def drawOverlayImage(self, img_path, x, y):
overlay_pixmap = QPixmap(img_path)
overlay_item = QGraphicsPixmapItem(overlay_pixmap)
overlay_item.setOffset(x, y)
self.graphicsScene.addItem(overlay_item)
def addLayer(self, layer_item, layer_name):
self.layersList.append(layer_item)
self.layersListWidget.addItem(layer_name)
self.updatePreview()
def closeEvent(self, event):
close = QMessageBox()
close.setWindowTitle("Close program")
close.setWindowIcon(QIcon("Resources/Icons/ACM_logo.jpg")) # Set the window icon
close.setText("Are you sure?")
close.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.Cancel)
close = close.exec()
if close == QMessageBox.StandardButton.Yes:
event.accept()
sys.exit()
else:
event.ignore()
# TODO: Tell user to press F for full screen
def keyPressEvent(self,event: QKeyEvent):
k = event.key()
txt = event.text()
alt_modifier = (event.modifiers() == Qt.KeyboardModifier.AltModifier)
if (txt == "f"):
if (not self.maximized):
self.maximized = True
self.showMaximized()
else:
self.maximized = False
self.showNormal()
def saveFile(self):
file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.FileMode.AnyFile)
file_dialog.setNameFilter("Images (*.png *.jpg *.bmp);;All Files (*)")
file_dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
if file_dialog.exec() != QFileDialog.DialogCode.Accepted:
return
file_name = file_dialog.selectedFiles()[0]
if file_name:
self.pixmap.save(file_name)