Skip to content

Commit

Permalink
2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
riceshowerX committed Jul 30, 2024
1 parent ec79da4 commit 136ba3e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 119 deletions.
149 changes: 36 additions & 113 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# gui.py
# gui.py
import os
import sys
from concurrent.futures import ThreadPoolExecutor

from PIL import Image
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 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__()
Expand Down Expand Up @@ -91,63 +87,32 @@ def init_ui(self):

def select_source(self):
if self.single_file_mode:
self.source_button.clicked.disconnect()
self.source_button.clicked.connect(self.select_single_file)
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}")
else:
self.source_button.clicked.disconnect()
self.source_button.clicked.connect(self.select_source_directory)
self.source_button.click()

def select_single_file(self):
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_directory = QFileDialog.getExistingDirectory(self, "选择原始图片文件夹")
if self.source_directory:
self.source_label.setText(f"已选择文件夹: {self.source_directory}")

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:
if not self.source_file or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择文件和目标文件夹!")
return

prefix = self.prefix_entry.text()
try:
start_number = int(self.start_number_entry.text())
except ValueError:
QMessageBox.warning(self, "警告", "起始编号必须是一个整数!")
return

try:
file_name, file_ext = os.path.splitext(os.path.basename(self.source_file))
new_file_name = f"{prefix}{start_number}{file_ext}"
new_file_path = os.path.join(self.target_directory, new_file_name)
os.rename(self.source_file, new_file_path)
QMessageBox.information(self, "结果", f"重命名完成!")
except Exception as e:
QMessageBox.warning(self, "错误", f"发生错误: {e}")
else:
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return

prefix = self.prefix_entry.text()
try:
start_number = int(self.start_number_entry.text())
except ValueError:
QMessageBox.warning(self, "警告", "起始编号必须是一个整数!")
return
prefix = self.prefix_entry.text()
try:
start_number = int(self.start_number_entry.text())
except ValueError:
QMessageBox.warning(self, "警告", "起始编号必须是一个整数!")
return

self.executor.submit(self._batch_rename_task, self.source_directory, self.target_directory, prefix, start_number)
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:
Expand All @@ -163,36 +128,16 @@ def _batch_rename_task(self, source_directory, target_directory, prefix, start_n
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()
if not target_format:
QMessageBox.warning(self, "警告", "请指定目标格式!")
return

try:
file_name, _ = os.path.splitext(os.path.basename(self.source_file))
new_file_name = f"{file_name}.{target_format}"
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, "结果", "转换完成!")
except Exception as e:
QMessageBox.warning(self, "错误", f"发生错误: {e}")
else:
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return

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

self.executor.submit(self._batch_convert_task, self.source_directory, self.target_directory, target_format)
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:
Expand All @@ -207,36 +152,17 @@ def _batch_convert_task(self, source_directory, target_directory, target_format)
self.show_error_message(f"发生错误: {e}")

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

try:
quality = int(self.quality_entry.text())
except ValueError:
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, "结果", "压缩完成!")
except Exception as e:
QMessageBox.warning(self, "错误", f"发生错误: {e}")
else:
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return
if not self.source_directory or not self.target_directory:
QMessageBox.warning(self, "警告", "请先选择原始和保存图片文件夹!")
return

try:
quality = int(self.quality_entry.text())
except ValueError:
QMessageBox.warning(self, "警告", "压缩质量必须是一个整数!")
return
try:
quality = int(self.quality_entry.text())
except ValueError:
QMessageBox.warning(self, "警告", "压缩质量必须是一个整数!")
return

self.executor.submit(self._batch_compress_task, self.source_directory, self.target_directory, quality)
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:
Expand All @@ -260,21 +186,18 @@ def toggle_file_mode(self, state):
self.source_label.setText("选择原始图片文件:")
else:
self.source_label.setText("选择原始图片文件夹:")
self.select_source()

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

def show_error_message(self, message):
QMessageBox.warning(self, "错误", message)


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


if __name__ == '__main__':
main()
main()
Binary file modified utils/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
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.
1 change: 0 additions & 1 deletion utils/error_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# utils/error_handler.py
from PyQt6.QtWidgets import QMessageBox


def handle_error(error_message):
"""显示错误信息弹窗。
Expand Down
5 changes: 0 additions & 5 deletions utils/image_processor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# utils/image_processor.py
import os

from PIL import Image

from . import error_handler


def batch_rename_files(source_dir, target_dir, prefix, start_number, progress_callback=None):
count = 0
for root, _, files in os.walk(source_dir):
Expand All @@ -27,7 +24,6 @@ def batch_rename_files(source_dir, target_dir, prefix, start_number, progress_ca
error_handler.handle_error(f"重命名文件 {file} 时出错: {e}")
return count


def batch_convert_images(source_dir, target_dir, target_format, progress_callback=None):
count = 0
for root, _, files in os.walk(source_dir):
Expand All @@ -48,7 +44,6 @@ def batch_convert_images(source_dir, target_dir, target_format, progress_callbac
error_handler.handle_error(f"转换文件 {file} 时出错: {e}")
return count


def batch_compress_images(source_dir, target_dir, quality, progress_callback=None):
count = 0
for root, _, files in os.walk(source_dir):
Expand Down

0 comments on commit 136ba3e

Please sign in to comment.