-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
194 lines (154 loc) · 7.16 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
import sys
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QPushButton, QFileDialog, QLabel,
QProgressBar, QComboBox, QHBoxLayout, QLineEdit
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from PIL import Image
import os
class ConversionThread(QThread):
progress_update = pyqtSignal(int)
status_update = pyqtSignal(str)
def __init__(self, images, output_dir, output_format, output_resolution):
super().__init__()
self.images = images
self.output_dir = output_dir
self.output_format = output_format
self.output_resolution = output_resolution
def run(self):
total_images = len(self.images)
for i, img_path in enumerate(self.images):
try:
# Log which image is being processed
self.status_update.emit(f"Processing {os.path.basename(img_path)}...")
# Open and convert the image
img = Image.open(img_path)
img = img.resize(self.output_resolution) # Resize the image
# Create the new file name
base_name = os.path.basename(img_path)
new_file_name = os.path.splitext(base_name)[0] + f"_converted.{self.output_format}"
save_path = os.path.join(self.output_dir, new_file_name)
# Save the image in the specified format
img.save(save_path, format=self.output_format.upper())
# Update progress
progress = int(((i + 1) / total_images) * 100)
self.progress_update.emit(progress)
except Exception as e:
# If there's an error, log it but continue with the next image
self.status_update.emit(f"Error with {os.path.basename(img_path)}: {str(e)}")
continue # Skip to the next image
self.status_update.emit("Conversion complete!")
class MiniPixelApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Set fixed window size
self.setFixedSize(500, 400)
self.setWindowTitle('Mini Pixel')
# Set window toolbar icon
self.setWindowIcon(QIcon('icons/window_icon.png'))
# Layout
layout = QVBoxLayout()
# Label to display status
self.status_label = QLabel("Select images to convert", self)
layout.addWidget(self.status_label)
# Button to select images with icon
self.select_button = QPushButton('Select Images', self)
self.select_button.setIcon(QIcon('icons/select_images.png'))
self.select_button.clicked.connect(self.select_images)
layout.addWidget(self.select_button)
# Dropdown to select resolution
self.resolution_label = QLabel("Select Resolution", self)
layout.addWidget(self.resolution_label)
self.resolution_combo = QComboBox(self)
self.resolution_combo.addItems([
"800x600", "1024x768", "1280x720", "1920x1080", "Custom"
])
layout.addWidget(self.resolution_combo)
# Custom resolution input fields
self.custom_res_layout = QHBoxLayout()
self.width_input = QLineEdit(self)
self.width_input.setPlaceholderText("Width")
self.custom_res_layout.addWidget(self.width_input)
self.height_input = QLineEdit(self)
self.height_input.setPlaceholderText("Height")
self.custom_res_layout.addWidget(self.height_input)
layout.addLayout(self.custom_res_layout)
# Dropdown to select format
self.format_label = QLabel("Select Format", self)
layout.addWidget(self.format_label)
self.format_combo = QComboBox(self)
self.format_combo.addItems([
"PNG", "JPG", "JPEG", "BMP"
])
layout.addWidget(self.format_combo)
# Button to convert images with icon
self.convert_button = QPushButton('Convert Images', self)
self.convert_button.setIcon(QIcon('icons/convert_images.png'))
self.convert_button.setEnabled(False) # Disabled until images are selected
self.convert_button.clicked.connect(self.convert_images)
layout.addWidget(self.convert_button)
# Progress bar
self.progress_bar = QProgressBar(self)
layout.addWidget(self.progress_bar)
# Label to show percentage
self.percentage_label = QLabel("0%", self)
layout.addWidget(self.percentage_label)
# Set the layout
self.setLayout(layout)
# Store selected images
self.selected_images = []
def select_images(self):
# Open file dialog to select multiple images
options = QFileDialog.Options()
files, _ = QFileDialog.getOpenFileNames(self, "Select Images", "", "Images (*.png *.jpg *.jpeg *.bmp)", options=options)
if files:
self.selected_images = files
self.status_label.setText(f"{len(files)} images selected")
self.convert_button.setEnabled(True)
def convert_images(self):
if not self.selected_images:
return
# Ask the user to select an output directory
output_dir = QFileDialog.getExistingDirectory(self, "Select Output Directory")
if not output_dir:
return # If the user cancels, do nothing
# Check if custom resolution is selected
if self.resolution_combo.currentText() == "Custom":
try:
# Get the custom width and height
custom_width = int(self.width_input.text())
custom_height = int(self.height_input.text())
output_resolution = (custom_width, custom_height)
except ValueError:
# If input is invalid, show an error
self.status_label.setText("Error: Invalid custom resolution values.")
return
else:
# Get selected resolution from dropdown
selected_resolution = self.resolution_combo.currentText()
width, height = map(int, selected_resolution.split('x'))
output_resolution = (width, height)
# Get selected format
output_format = self.format_combo.currentText().lower()
# Create and start the conversion thread
self.thread = ConversionThread(self.selected_images, output_dir, output_format, output_resolution)
self.thread.progress_update.connect(self.update_progress)
self.thread.status_update.connect(self.update_status)
self.thread.start()
def update_progress(self, value):
# Update progress bar and percentage
self.progress_bar.setValue(value)
self.percentage_label.setText(f"{value}%")
def update_status(self, message):
# Update status label
self.status_label.setText(message)
if message == "Conversion complete!":
self.convert_button.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MiniPixelApp()
ex.show()
sys.exit(app.exec_())