import os
import shutil
from PyQt5 import QtWidgets, QtGui
from PIL import Image
import imagehash
from PyQt5.QtCore import QThread, pyqtSignal
class FileMoverApp(QtWidgets.QWidget):
def __init__(self):
super().__init__()
# 设置窗口标题和大小
self.setWindowTitle('重复图片检测工具 https://www.jinsuitui.com')
self.resize(600, 400) # 仅设置窗口大小
# 此处省去样式代码
def run_file_mover(self):
self.log_output.clear() # 清空之前的日志信息
content_folder = self.content_folder_input.text()
result_folder = self.result_folder_input.text()
if not content_folder or not result_folder:
self.log_output.append("文件夹路径不能为空!")
return # 如果文件夹路径为空,则直接返回,不执行任何操作
if not os.path.exists(result_folder):
os.makedirs(result_folder)
image_files = [f for f in os.listdir(content_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
if not image_files:
self.log_output.append("初始文件夹中没有找到任何图片!")
return # 如果没有图片则直接返回
self.thread = ImageProcessor(content_folder, result_folder)
self.thread.log_signal.connect(self.log_output.append) # 连接信号到日志输出
self.thread.start() # 启动线程
class ImageProcessor(QThread):
log_signal = pyqtSignal(str) # 信号用于发送日志信息
def __init__(self, content_folder, result_folder):
super().__init__()
self.content_folder = content_folder
self.result_folder = result_folder
def run(self):
image_files = [f for f in os.listdir(self.content_folder) if
f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
hash_dict = {}
duplicates_found = False
for file in image_files:
file_path = os.path.join(self.content_folder, file)
try:
image = Image.open(file_path)
image_hash = imagehash.phash(image)
if image_hash in hash_dict:
duplicates_found = True
existing_file, existing_resolution = hash_dict[image_hash]
current_resolution = image.size
if current_resolution[0] * current_resolution[1] > existing_resolution[0] * existing_resolution[1]:
hash_dict[image_hash] = (file_path, current_resolution)
shutil.move(existing_file, self.result_folder)
self.log_signal.emit(f"{os.path.basename(existing_file)},检测到重复,移动成功!")
else:
shutil.move(file_path, self.result_folder)
self.log_signal.emit(f"{os.path.basename(file_path)},检测到重复,移动成功!")
else:
hash_dict[image_hash] = (file_path, image.size)
except Exception:
pass # 不记录错误信息
if not duplicates_found:
self.log_signal.emit("没有找到重复的图片!")
if __name__ == '__main__':
app = QtWidgets.QApplication([])
file_mover_app = FileMoverApp()
file_mover_app.show()
app.exec_()原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。