主要功能是创建一个基于Tkinter的图像浏览器程序,允许用户在指定的根目录下浏览图像文件夹中的图像文件,并具有以下功能:
-
图像浏览:用户可以使用左箭头和右箭头键来切换查看下一张和上一张图像。
-
图像标记:用户可以按空格键来标记当前查看的图像,将其文件路径添加到已标记的图像列表中,并将标记信息保存到"plot.txt"文件中。
-
保存浏览状态:程序会在窗口关闭时保存当前浏览状态,包括当前所在文件夹和图像索引,以便下次重新打开程序时能够恢复到上次浏览的状态,并将标记信息保存到"last_viewed.txt"文件中。
-
显示图像信息:程序会在图像上方显示当前图像的文件路径和图像索引信息。
-
自动排序:程序会自动将图像文件夹中的图像按数字顺序进行排序。
import os
import re
import tkinter as tk
from PIL import Image, ImageTk, ImageDraw
from tkinter import PhotoImage
from itertools import chain
def numerical_sort(value):
numbers = re.compile(r'(\d+)')
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
class ImageBrowser:
def __init__(self, root_dir):
self.root_dir = root_dir
self.image_folders = self._gather_image_folders()
self.current_folder_index, self.current_image_index = self._load_last_viewed()
self.marked_images = []
self.window = tk.Tk()
self.window.title("Image Browser")
self.image_label = tk.Label(self.window)
self.image_label.pack()
self.window.bind("<Left>", self.prev_image)
self.window.bind("<Right>", self.next_image)
self.window.bind("<space>", self.mark_image)
self.window.protocol("WM_DELETE_WINDOW", self._on_close)
self.update_image()
def _gather_image_folders(self):
image_folders = []
for subdir, dirs, files in os.walk(self.root_dir):
if os.path.basename(subdir) == 'plot':
sorted_files = sorted([file for file in files if file.endswith('.jpg')], key=numerical_sort)
image_paths = [os.path.join(subdir, file) for file in sorted_files]
if image_paths:
image_folders.append(image_paths)
return image_folders
def _load_last_viewed(self):
try:
with open("last_viewed.txt", "r") as file:
folder_index, image_index = map(int, file.readline().split(','))
if folder_index < len(self.image_folders) and image_index < len(self.image_folders[folder_index]):
return folder_index, image_index
except Exception:
pass
return 0, 0
def _save_last_viewed(self):
with open("last_viewed.txt", "w") as file:
file.write(f"{self.current_folder_index},{self.current_image_index}")
def _on_close(self):
self._save_last_viewed()
self.window.destroy()
def update_image(self):
if self.image_folders:
current_folder = self.image_folders[self.current_folder_index]
image_path = current_folder[self.current_image_index]
img = Image.open(image_path)
img.thumbnail((800, 600), Image.ANTIALIAS)
draw = ImageDraw.Draw(img)
draw.text((10, 10), image_path, fill="red")
draw.text((img.width - 70, 10), f"{self.current_image_index + 1}/{len(current_folder)}", fill="red")
photo = ImageTk.PhotoImage(img)
self.image_label.config(image=photo)
self.image_label.image = photo
def next_image(self, event=None):
current_folder = self.image_folders[self.current_folder_index]
if self.current_image_index < len(current_folder) - 1:
self.current_image_index += 1
else:
if self.current_folder_index < len(self.image_folders) - 1:
self.current_folder_index += 1
self.current_image_index = 0
self.update_image()
def prev_image(self, event=None):
if self.current_image_index > 0:
self.current_image_index -= 1
else:
if self.current_folder_index > 0:
self.current_folder_index -= 1
current_folder = self.image_folders[self.current_folder_index]
self.current_image_index = len(current_folder) - 1
self.update_image()
def mark_image(self, event=None):
current_folder = self.image_folders[self.current_folder_index]
marked_image_path = current_folder[self.current_image_index]
if marked_image_path not in self.marked_images:
self.marked_images.append(marked_image_path)
with open("plot.txt", "a") as file:
file.write(marked_image_path + "\n")
def run(self):
self.window.mainloop()
# 写入输入路径
image_browser = ImageBrowser("")
image_browser.run()
根据plot.txt内容,可以删除不合格图片
import os
def delete_files_from_list(file_list_path):
try:
with open(file_list_path, 'r') as file:
for line in file:
file_path = line.strip()
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted: {file_path}")
else:
print(f"File not found: {file_path}")
except FileNotFoundError:
print(f"The file {file_list_path} does not exist.")
delete_files_from_list("plot.txt")