图片筛选py转exe

图片筛选py转exe,第1张

图片筛选py转exe
from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QApplication, QVBoxLayout, QHBoxLayout
from PyQt5.QtWidgets import QInputDialog, QMessageBox, QDialog, QRadioButton, QButtonGroup, QProgressBar
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QRect
import sys
import os
import shutil
import time
import json
import sys


class ImagePng(object):
    def __init__(self, png_src_path):
        super().__init__()

        self.png_src_path = png_src_path
        self.png_list = sorted([png for png in os.listdir(self.png_src_path) if png.endswith('.png')],
                               key=lambda x: '-'.join(
                                   x.split('-')[:-1] + [os.path.splitext(x.split('-')[-1])[0].zfill(5)]))
        self.delete_list = []
        self.index_png = 0
        if os.path.exists("note.json"):
            with open('note.json', 'r') as f:
                json_file = json.load(f)
            if json_file["file"] == self.png_src_path:
                self.png_list = json_file["png_list"]
                self.delete_list = json_file["delete_list"]
                if self.delete_list:
                    self.index_png = self.delete_list[-1][0]

    def next_png(self):
        if self.index_png < len(self.png_list) - 1:
            self.index_png += 1
        else:
            return False
        return True

    def prev_png(self):
        if self.index_png > 0:
            self.index_png -= 1
        else:
            return False
        return True

    def pop_png(self):
        if self.at_end_png():
            self.delete_list.append((self.index_png, self.png_list.pop(self.index_png)))
            self.index_png -= 1
        else:
            self.delete_list.append((self.index_png, self.png_list.pop(self.index_png)))

    def delete_png(self):
        if self.png_list[self.index_png] == self.png_list[-1]:
            for png_path in self.delete_list:
                if os.path.exists(os.path.join(self.png_src_path, png_path[1])):
                    os.unlink(os.path.join(self.png_src_path, png_path[1]))
                    os.unlink(os.path.join(self.png_src_path, os.path.splitext(png_path[1])[0] + ".raw"))
                else:
                    print("can't not find the png: ", os.path.join(self.png_src_path, self.png_list[self.index_png]))

    def get_png_path(self):
        return os.path.join(self.png_src_path, self.png_list[self.index_png])

    def get_png_file_name(self):
        return self.png_list[self.index_png]

    def at_end_png(self):
        return self.png_list[self.index_png] == self.png_list[-1]

    def undo(self):
        if self.delete_list:
            index, path = self.delete_list.pop()
            self.png_list.insert(index, path)

    def get_png_list_len(self):
        return len(self.png_list)

    def get_current_index(self):
        return self.index_png


class Selected(QWidget):
    def __init__(self, png_src_path):
        super().__init__()
        self.png_src_path = png_src_path
        self.png_list = [png for png in os.listdir(png_src_path) if png.endswith('.png')]
        self.delete_path = png_src_path + '_delete'
        if not os.path.exists(self.delete_path):
            os.makedirs(self.delete_path)
        self.rescale = 1.2
        # self.image_size = (800 // self.rescale, 672 // self.rescale)
        self.image_size = (1280 // self.rescale, 960 // self.rescale)
        self.delete_list = []
        self.select_message = ""
        self.select_start = None
        self.current_index = 0
        self.image = os.path.join(self.png_src_path, self.png_list[self.current_index])
        self.initUI()

    def initUI(self):
        self.png_path_label = QLabel(self)
        self.png_path_label.setText(self.image)

        self.message_label = QLabel(self)
        self.message_label.setText("Info: Selected Png")

        png_img = QPixmap(self.image)
        # png_img = png_img.copy(QRect(800, 0, 2000, 1672))
        png_img = png_img.scaled(*self.image_size)
        self.png_box = QLabel(self)
        self.png_box.setPixmap(png_img)

        self.png_progress = QProgressBar()
        self.png_progress.setValue(self.current_index)
        self.png_progress.setRange(0, len(self.png_list) - 1)
        self.png_progress.setAlignment(Qt.AlignVCenter)

        main_box = QVBoxLayout()

        main_box.addWidget(self.png_path_label)
        main_box.addWidget(self.png_box)
        main_box.addWidget(self.png_progress)
        main_box.addWidget(self.message_label)
        self.setLayout(main_box)

        self.setGeometry(500, 100, 1200, 800)  # ini show in the desk
        self.setWindowTitle("Selected_png")
        self.setFocus()
        self.show()

    def button_next_clicked(self):
        if self.current_index == len(self.png_list) - 1:
            self.message_label.setText("Warning: At the last png")
            return
        self.current_index += 1
        self.png_progress.setValue(self.current_index)
        self.message_label.setText("Info: Show Png" + self.select_message)
        self._show_png()

    def button_prev_clicked(self):
        if self.current_index == 0:
            self.message_label.setText("Warning: At the first png")
            return
        self.current_index -= 1
        self.png_progress.setValue(self.current_index)
        self.message_label.setText("Info: Show Png" + self.select_message)
        self._show_png()

    def button_select_clicked(self):
        if self.select_start is None:
            self.select_start = self.current_index
            self.select_message = "  Select from: " + self.png_list[self.current_index]
            self.message_label.setText("Info: Selected Png" + self.select_message)
            self.button_next_clicked()
        else:
            if self.select_start > self.current_index:
                self.message_label.setText("Error: Delete Selected Png" + self.select_message)
            else:
                delete_tmp = []
                for i in range(self.select_start, self.current_index + 1):
                    delete_tmp.append(self.png_list[i])
                for png_name in delete_tmp:
                    shutil.move(os.path.join(self.png_src_path, png_name),
                                os.path.join(self.delete_path, png_name))
                    self.message_label.setText("Info: Delete Complete" + png_name)
                    self.png_list.remove(png_name)
                self.delete_list.append(delete_tmp)
                self.current_index = self.select_start
                self.png_progress.setValue(self.current_index)
                self.png_progress.setRange(0, len(self.png_list) - 1)
            self.select_start = None
            self.select_message = ""
        self._show_png()

    def button_delete_clicked(self):
        if self.select_start is not None:
            self.message_label.setText("Error: At the Selecte Mode" + self.select_message)
            return
        shutil.move(os.path.join(self.png_src_path, self.png_list[self.current_index]),
                    os.path.join(self.delete_path, self.png_list[self.current_index]))
        self.message_label.setText("Info: Delete Complete" + self.png_list[self.current_index])
        png_name = self.png_list.pop(self.current_index)
        self.delete_list.append([png_name])
        self.png_progress.setValue(self.current_index)
        self.png_progress.setRange(0, len(self.png_list) - 1)
        self._show_png()

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_A or key == Qt.Key_Left:
            self.button_prev_clicked()
        elif key == Qt.Key_D or key == Qt.Key_Right:
            self.button_next_clicked()
        elif key == Qt.Key_W or key == Qt.Key_Up:
            self.button_select_clicked()
        elif key == Qt.Key_Delete:
            self.button_delete_clicked()
        elif key == Qt.Key_U:
            self._undo_action()

    def _undo_action(self):
        if self.delete_list:
            pass

    def _show_png(self):
        self.image = os.path.join(self.png_src_path, self.png_list[self.current_index])
        png_img = QPixmap(self.image)
        # png_img = png_img.copy(QRect(800, 0, 2000, 1672))
        png_img = png_img.scaled(*self.image_size)
        self.png_box.setPixmap(png_img)
        self.png_path_label.setText(self.image)


if __name__ == "__main__":
    png_src_path = r"J:intershiplocal_dataGpytorchIMG_correcttest1"
    app = QApplication(sys.argv)
    ex = Selected(png_src_path)
    sys.exit(app.exec_())

1.安装

   Anaconda (这里包含了Qt Designer、PyUIC5、PyRCC5)

    PyCharm  (激活)

    Pyinstaller (pip install pyinstaller 或者 pip3 install pyinstaller)

    PyQt5  (pip install pyqt5 或者 pip3 install pyqt5)

    Qt Designer:Qt设计师,设计界面

    PyUIC5:设计的界面转换成 xxx.py

    PyRCC5:资源文件转换成 xxx.py

    Pyinstaller:打包 xxx.py 为 xxx.exe

如果出现 importError: cannot import name ‘PackagePath‘ from ‘importlib_metadata‘

importlib_metadata版本太老了,升级一下

pip install importlib_metadata --upgrade

打包成exe

pyinstaller -F xxx.py

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5490895.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存