• 小工具——筛选图像小工具


    最近在公司手动筛图片,需要将某些含有检测目标的图像手动筛选出来用于做新模型的测试。我最开始是两个文件夹,来回复制粘贴,后来感觉这种效率太低了,就随手写了一个图像筛查小工具。代码如下:

    1. import sys
    2. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QAction, QFileDialog
    3. from PyQt5.QtGui import QPixmap, QIcon, QImage
    4. from PyQt5.QtCore import Qt, QDir
    5. class ImageViewer(QMainWindow):
    6. def __init__(self):
    7. super().__init__()
    8. self.current_index = 0
    9. self.image_files = []
    10. self.input_folder_path = ""
    11. self.output_folder_path = ""
    12. self.init_ui()
    13. def init_ui(self):
    14. # 创建布局
    15. layout = QVBoxLayout()
    16. # 创建标签用于显示图像
    17. self.image_label = QLabel(self)
    18. layout.addWidget(self.image_label)
    19. # 创建主窗口
    20. main_widget = QWidget()
    21. main_widget.setLayout(layout)
    22. self.setCentralWidget(main_widget)
    23. # 创建菜单栏
    24. menu_bar = self.menuBar()
    25. file_menu = menu_bar.addMenu("文件")
    26. self.open_input_folder_action = QAction("打开导入图像文件夹", self)
    27. self.open_output_folder_action = QAction("打开保存图像文件夹", self)
    28. file_menu.addAction(self.open_input_folder_action)
    29. file_menu.addAction(self.open_output_folder_action)
    30. # 绑定事件处理函数
    31. self.open_input_folder_action.triggered.connect(self.open_input_folder)
    32. self.open_output_folder_action.triggered.connect(self.open_output_folder)
    33. # 设置窗口属性
    34. self.setWindowTitle("图像筛查工具")
    35. self.setGeometry(100, 100, 800, 600)
    36. def open_input_folder(self):
    37. # 打开输入文件夹
    38. folder_path = QFileDialog.getExistingDirectory(self, "选择输入文件夹", QDir.homePath())
    39. if folder_path:
    40. self.input_folder_path = folder_path
    41. self.load_images()
    42. def open_output_folder(self):
    43. # 打开输出文件夹
    44. folder_path = QFileDialog.getExistingDirectory(self, "选择输出文件夹", QDir.homePath())
    45. if folder_path:
    46. self.output_folder_path = folder_path
    47. def load_images(self):
    48. # 加载文件夹中的图像文件
    49. self.image_files = QDir(self.input_folder_path).entryList(["*.png", "*.jpg", "*.jpeg"], QDir.Files)
    50. if self.image_files:
    51. self.current_index = 0
    52. self.show_current_image()
    53. def show_current_image(self):
    54. # 显示当前图像
    55. image_path = QDir(self.input_folder_path).filePath(self.image_files[self.current_index])
    56. image = QImage(image_path)
    57. if not image.isNull():
    58. pixmap = QPixmap.fromImage(image)
    59. self.image_label.setPixmap(pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio,
    60. Qt.SmoothTransformation))
    61. self.setWindowTitle(f"图像筛查工具 - {self.current_index + 1}/{len(self.image_files)}")
    62. def keyPressEvent(self, event):
    63. # 键盘事件处理
    64. if event.key() == Qt.Key_Right:
    65. # 切换到下一张图像
    66. if self.current_index < len(self.image_files) - 1:
    67. self.current_index += 1
    68. self.show_current_image()
    69. elif event.key() == Qt.Key_Left:
    70. # 切换到上一张图像
    71. if self.current_index > 0:
    72. self.current_index -= 1
    73. self.show_current_image()
    74. elif event.key() == Qt.Key_Space:
    75. # 保存当前图像到输出文件夹
    76. if self.current_index < len(self.image_files):
    77. image_path = QDir(self.input_folder_path).filePath(self.image_files[self.current_index])
    78. output_path = QDir(self.output_folder_path).filePath(self.image_files[self.current_index])
    79. QDir().rename(image_path, output_path)
    80. self.image_files.pop(self.current_index)
    81. self.show_current_image()
    82. if __name__ == "__main__":
    83. app = QApplication(sys.argv)
    84. viewer = ImageViewer()
    85. viewer.show()
    86. sys.exit(app.exec_())

    首先要选择两个文件夹,一个用来导入数据,另外一个用来保存数据。导入文件夹,文件夹里面有大量图像,点击键盘右键可以切换到下一张,左键上一张,点击空格可以把这个图像保存到另外一个文件夹中。不过到最后一张的时候可能回异常退出,不过不影响使用所以我就没进行改进了。

    下面是该工具的效果:

    点击2选择源图像文件夹

    点击3选择保存文件夹

    这里的操作是剪切操作,并非复制操作。

     随便文件夹是保存的文件

  • 相关阅读:
    计算机图形学线性代数相关概念
    Spring框架在Bean中的管理(第十课)
    deepin开发web前端:探索、挑战与无限可能
    3个变化3秒区别MT4和MT5
    ubuntu22sshd服务保持连接的设置
    用Python实现广度优先搜索
    Frontiers | 北林邬荣领/何晓青-网络作图揭示拟南芥与叶际微生物组互作机制
    STL-vector
    【性能测试】稳定性/并发压力测试的TPS计算+5W并发场景设计...
    基于SSM的焦作旅游协会管理系统设计与实现
  • 原文地址:https://blog.csdn.net/qq_54932411/article/details/132757180