Skip to content

Commit

Permalink
2.3
Browse files Browse the repository at this point in the history
fix many bug
  • Loading branch information
riceshowerX committed Jul 29, 2024
1 parent 37e6f8c commit ec79da4
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 123 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/SnapForge.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

174 changes: 90 additions & 84 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
# gui.py
from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog, QVBoxLayout, QPushButton, QLineEdit, QLabel, QWidget, QMessageBox, QProgressBar, QCheckBox
from PyQt6.QtCore import Qt
from utils import image_processor
import os
import sys
from PyQt6.QtGui import QIcon
from concurrent.futures import ThreadPoolExecutor

from PIL import Image
import os
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog, QVBoxLayout, QPushButton, QLineEdit, QLabel, \
QWidget, QMessageBox, QProgressBar, QCheckBox

from utils import image_processor


class ImageProcessingApp(QMainWindow):
def __init__(self):
super().__init__()

# 设置窗口标题
self.setWindowTitle("SnapForge")

# 设置窗口图标
self.setWindowTitle("SnapForge")
app_icon = QIcon("snapforge.ico")
self.setWindowIcon(app_icon)

self.resize(400, 380)
self.setWindowIcon(app_icon)
self.resize(400, 380)

self.source_directory = None
self.target_directory = None
self.single_file_mode = False
self.source_file = None
self.source_file = None

self.executor = ThreadPoolExecutor(max_workers=1) # 初始化线程池

self.init_ui()

def init_ui(self):
layout = QVBoxLayout()

# 单个文件处理模式选择
self.single_file_checkbox = QCheckBox("单个文件处理")
self.single_file_checkbox.stateChanged.connect(self.toggle_file_mode)
layout.addWidget(self.single_file_checkbox)

# 选择文件夹/文件
self.source_label = QLabel("选择原始图片文件夹/文件:")
self.source_button = QPushButton("选择")
# 注意: 这里不再设置按钮的连接
self.source_button.clicked.connect(self.select_source)
layout.addWidget(self.source_label)
layout.addWidget(self.source_button)

Expand All @@ -48,7 +49,6 @@ def init_ui(self):
layout.addWidget(self.target_label)
layout.addWidget(self.target_button)

# 批量重命名
self.prefix_label = QLabel("前缀:")
self.prefix_entry = QLineEdit()
self.start_number_label = QLabel("起始编号:")
Expand All @@ -62,7 +62,6 @@ def init_ui(self):
layout.addWidget(self.start_number_entry)
layout.addWidget(self.rename_button)

# 批量转换
self.format_label = QLabel("目标格式 (如 jpeg, png):")
self.format_entry = QLineEdit()
self.convert_button = QPushButton("执行转换")
Expand All @@ -72,7 +71,6 @@ def init_ui(self):
layout.addWidget(self.format_entry)
layout.addWidget(self.convert_button)

# 批量压缩
self.quality_label = QLabel("压缩质量 (1-100):")
self.quality_entry = QLineEdit("80")
self.compress_button = QPushButton("执行压缩")
Expand All @@ -82,7 +80,6 @@ def init_ui(self):
layout.addWidget(self.quality_entry)
layout.addWidget(self.compress_button)

# 进度条
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
self.progress_bar.setAlignment(Qt.AlignmentFlag.AlignCenter)
Expand All @@ -99,21 +96,23 @@ def select_source(self):
else:
self.source_button.clicked.disconnect()
self.source_button.clicked.connect(self.select_source_directory)

self.source_button.click()
self.source_button.click()

def select_single_file(self):
self.source_file, _ = QFileDialog.getOpenFileName(self, "选择图片文件", filter="Image Files (*.png *.jpg *.jpeg *.bmp *.tiff)")
self.source_file, _ = QFileDialog.getOpenFileName(self, "选择图片文件",
filter="Image Files (*.png *.jpg *.jpeg *.bmp *.tiff)")
if self.source_file:
self.source_label.setText(f"已选择文件: {self.source_file}")

def select_source_directory(self):
self.source_directory = QFileDialog.getExistingDirectory(self, "选择原始图片文件夹")
if self.source_directory:
self.source_label.setText(f"已选择文件夹: {self.source_directory}") # 添加此行
self.source_label.setText(f"已选择文件夹: {self.source_directory}")

def select_target_directory(self):
def select_target_directory(self):
self.target_directory = QFileDialog.getExistingDirectory(self, "选择保存图片文件夹")
if self.target_directory:
self.target_label.setText(f"已选择文件夹: {self.target_directory}")

def rename_files(self):
if self.single_file_mode:
Expand All @@ -136,7 +135,7 @@ def rename_files(self):
QMessageBox.information(self, "结果", f"重命名完成!")
except Exception as e:
QMessageBox.warning(self, "错误", f"发生错误: {e}")
else: # 批量处理模式
else:
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return
Expand All @@ -148,28 +147,30 @@ def rename_files(self):
QMessageBox.warning(self, "警告", "起始编号必须是一个整数!")
return

try:
renamed_count = image_processor.batch_rename_files(
self.source_directory,
self.target_directory,
prefix,
start_number,
self.update_progress_bar
)
QMessageBox.information(self, "结果", f"重命名完成!共重命名了 {renamed_count} 个文件。")
except Exception as e:
# 处理其他潜在的异常
QMessageBox.warning(self, "错误", f"发生错误: {e}")
self.executor.submit(self._batch_rename_task, self.source_directory, self.target_directory, prefix, start_number)

def _batch_rename_task(self, source_directory, target_directory, prefix, start_number):
try:
renamed_count = image_processor.batch_rename_files(
source_directory,
target_directory,
prefix,
start_number,
self.update_progress_bar
)
self.show_info_message(f"重命名完成!共重命名了 {renamed_count} 个文件。")
except Exception as e:
self.show_error_message(f"发生错误: {e}")

def convert_files(self):
if self.single_file_mode:
if not self.source_file or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择文件和目标文件夹!")
return

target_format = self.format_entry.text().lower()
target_format = self.format_entry.text()
if not target_format:
QMessageBox.warning(self, "警告", "目标格式不能为空!")
QMessageBox.warning(self, "警告", "请指定目标格式!")
return

try:
Expand All @@ -178,30 +179,32 @@ def convert_files(self):
new_file_path = os.path.join(self.target_directory, new_file_name)
img = Image.open(self.source_file)
img.save(new_file_path, target_format.upper())
QMessageBox.information(self, "结果", f"转换完成!")
QMessageBox.information(self, "结果", "转换完成!")
except Exception as e:
QMessageBox.warning(self, "错误", f"发生错误: {e}")
else: # 批量处理模式
else:
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return

target_format = self.format_entry.text().lower()
target_format = self.format_entry.text()
if not target_format:
QMessageBox.warning(self, "警告", "目标格式不能为空!")
QMessageBox.warning(self, "警告", "请指定目标格式!")
return

try:
converted_count = image_processor.batch_convert_images(
self.source_directory,
self.target_directory,
target_format,
self.update_progress_bar
)
QMessageBox.information(self, "结果", f"格式转换完成!共转换了 {converted_count} 个文件。")
except Exception as e:
# 处理其他潜在的异常
QMessageBox.warning(self, "错误", f"发生错误: {e}")
self.executor.submit(self._batch_convert_task, self.source_directory, self.target_directory, target_format)

def _batch_convert_task(self, source_directory, target_directory, target_format):
try:
converted_count = image_processor.batch_convert_images(
source_directory,
target_directory,
target_format,
self.update_progress_bar
)
self.show_info_message(f"转换完成!共转换了 {converted_count} 个文件。")
except Exception as e:
self.show_error_message(f"发生错误: {e}")

def compress_files(self):
if self.single_file_mode:
Expand All @@ -211,64 +214,67 @@ def compress_files(self):

try:
quality = int(self.quality_entry.text())
if quality < 1 or quality > 100:
raise ValueError
except ValueError:
QMessageBox.warning(self, "警告", "压缩质量必须是1到100之间的整数!")
QMessageBox.warning(self, "警告", "压缩质量必须是一个整数!")
return

try:
new_file_path = os.path.join(self.target_directory, os.path.basename(self.source_file))
img = Image.open(self.source_file)
img.save(new_file_path, optimize=True, quality=quality)
QMessageBox.information(self, "结果", f"压缩完成!")
QMessageBox.information(self, "结果", "压缩完成!")
except Exception as e:
QMessageBox.warning(self, "错误", f"发生错误: {e}")

else: # 批量处理模式
else:
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return

try:
quality = int(self.quality_entry.text())
if quality < 1 or quality > 100:
raise ValueError
except ValueError:
QMessageBox.warning(self, "警告", "压缩质量必须是1到100之间的整数!")
QMessageBox.warning(self, "警告", "压缩质量必须是一个整数!")
return

try:
compressed_count = image_processor.batch_compress_images(
self.source_directory,
self.target_directory,
quality,
self.update_progress_bar
)
QMessageBox.information(self, "结果", f"压缩完成!共压缩了 {compressed_count} 个文件。")
except Exception as e:
# 处理其他潜在的异常
QMessageBox.warning(self, "错误", f"发生错误: {e}")
self.executor.submit(self._batch_compress_task, self.source_directory, self.target_directory, quality)

def _batch_compress_task(self, source_directory, target_directory, quality):
try:
compressed_count = image_processor.batch_compress_images(
source_directory,
target_directory,
quality,
self.update_progress_bar
)
self.show_info_message(f"压缩完成!共压缩了 {compressed_count} 个文件。")
except Exception as e:
self.show_error_message(f"发生错误: {e}")

def update_progress_bar(self, current, total):
"""更新进度条。
Args:
current (int): 当前进度。
total (int): 总进度。
"""
self.progress_bar.setValue(int(current / total * 100))
self.progress_bar.setMaximum(total)
self.progress_bar.setValue(current)

def toggle_file_mode(self, state):
self.single_file_mode = state == Qt.CheckState.Checked
if self.single_file_mode:
self.source_label.setText("选择原始图片文件:")
else:
self.source_label.setText("选择原始图片文件夹:")
self.select_source()

def show_info_message(self, message):
QMessageBox.information(self, "结果", message)

if __name__ == "__main__":
def show_error_message(self, message):
QMessageBox.warning(self, "错误", message)


def main():
app = QApplication(sys.argv)
window = ImageProcessingApp()
window.show()
sys.exit(app.exec())
sys.exit(app.exec())


if __name__ == '__main__':
main()
Binary file modified utils/__pycache__/error_handler.cpython-312.pyc
Binary file not shown.
Binary file modified utils/__pycache__/image_processor.cpython-312.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion utils/error_handler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# utils/error_handler.py
from PyQt6.QtWidgets import QMessageBox


def handle_error(error_message):
"""显示错误信息弹窗。
Args:
error_message (str): 错误信息。
"""
QMessageBox.warning(None, "错误", error_message)
QMessageBox.warning(None, "错误", error_message)
Loading

0 comments on commit ec79da4

Please sign in to comment.