Python tkinker 界面设计与图标设置和图片文件打包成单个exe

1.  python 打包图片资源文件和逻辑文件,以及桌面应用图标

pyinstaller -F ae_awb_tool.py  -p png_resource.py -i 1.ico -w

 2. 完整代码如下。其作用是调用MTK 研发的图片信息dump工具,对图片AE 和AWB 进行分析归类到所属的目录中,并统计目录下文件个数,填充到最后一级文件夹名称开头。

3. 将图片文件转换为 py文件的脚本


import base64
import tkinter.messagebox as msgbox
def picture_to_py(pic_name):
    convert_pic_past = open("%s" % pic_name, 'rb')  # 这里传入要转换的图片
    base64_convert_str = base64.b64encode(convert_pic_past.read())  # base64读取(字符串)
    convert_pic_past.close() # 关闭读取
    convert_write_data = 'img = "%s"' % base64_convert_str.decode() # 处理
    a = open('%s.py' % pic_name.replace('.', '_'), 'w+')
    a.write(convert_write_data)
    a.close()
 
if __name__ == '__main__':
    picture = ["PCBico.png"]  # 写入你要转换的文件名,建议把该代码保存到与转换图片相同路径下
    for x in picture:
        picture_to_py(x)
    msgbox.showinfo("tip", "convert successfully.")
————————————————
版权声明:本文为CSDN博主「lagesan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lagesan/article/details/128415788

4. 主逻辑脚本内容如下: 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os,base64
from tkinter.messagebox import showinfo
import matplotlib.pyplot as plt
import numpy as np
import subprocess
import datetime
import re
from png_resource import img as img_pic
import tkinter as tk
from tkinter import filedialog

import shutil

starttime = datetime.datetime.now()

global debug_Tool
global photopath
def select_tool_dir():
    global debug_Tool
    debug_entry.set("")
    debug_Tool = filedialog.askopenfile(initialdir=os.getcwd()).name
    if debug_Tool.endswith("DebugParser.exe"):
        debug_entry.set(debug_Tool)
    if os.path.exists(os.getcwd()+"\default_path.txt")  == False:
        with open(os.getcwd() + "\default_path.txt", 'w+') as f_default_path:
            f_default_path.close()
    with open(os.getcwd()+"\default_path.txt", 'r+') as f_default_path:
        lines = f_default_path.readlines()
        file_contents = []
        for line in lines:
            if line.startswith("tool_deault_dir=") == False:
                file_contents.append(line)
        f_default_path.seek(0)
        for content in file_contents:
            f_default_path.write(content)
        path_str = "tool_deault_dir=" + debug_Tool+'\n'
        f_default_path.write(path_str)
        f_default_path.close()

def select_picture_path():
    global photopath
    photo_entry.set("")
    photopath = filedialog.askdirectory(initialdir=os.getcwd())  # 使用askdirectory函数选择文件夹
    photopath += "/"
    photo_entry.set(photopath)

    if os.path.exists(os.getcwd()+"\default_path.txt")  == False:
        with open(os.getcwd() + "\default_path.txt", 'w+') as f_default_path:
            f_default_path.close()
    with open(os.getcwd() + "\default_path.txt", 'r+') as f_default_path:
        lines = f_default_path.readlines()
        file_contents = []
        for line in lines:
            if line.startswith("photo_deault_dir=") == False:
                file_contents.append(line)
        f_default_path.seek(0)
        for content in file_contents:
            f_default_path.write(content)
        path_str = "photo_deault_dir=" + photopath+'\n'
        f_default_path.write(path_str)
        f_default_path.close()



def analyse(exifpath):
    data = {
        "name": str,
        "AE_TAG_REALBVX1000": str,
        "AE_TAG_HS_EVD": str,
        "AE_TAG_FACE_STS_SIZE": str,
        "AE_TAG_FLT_DR": str,
        "AE_TAG_FLT_FDY": str
    }

    AE_TAG_REALBVX1000 = []
    AE_TAG_HS_EVD = []
    AE_TAG_FACE_STS_SIZE = []
    AE_TAG_FLT_DR = []
    AE_TAG_REALBVX1000_Normal = []
    AE_TAG_HS_EVD_Normal = []
    # exif 目录

    result = {
        # 无人脸
        "bv -∞~-1500 & evd -∞~1000": [0, []],
        "bv -∞~-1500 & evd 1000~4200": [0, []],
        "bv -∞~-1500 & evd 4200~6300": [0, []],
        "bv -∞~-1500 & evd 6300+": [0, []],

        "bv -1500~500 & evd -∞~1000": [0, []],
        "bv -1500~500 & evd 1000~4200": [0, []],
        "bv -1500~500 & evd 4200~6300": [0, []],
        "bv -1500~500 & evd 6300+": [0, []],

        "bv 500~2500 & evd -∞~1000": [0, []],
        "bv 500~2500 & evd 1000~4200": [0, []],
        "bv 500~2500 & evd 4200~6300": [0, []],
        "bv 500~2500 & evd 6300+": [0, []],

        "bv 2500~5000 & evd -∞~1000": [0, []],
        "bv 2500~5000 & evd 1000~4200": [0, []],
        "bv 2500~5000 & evd 4200~6300": [0, []],
        "bv 2500~5000 & evd 6300+": [0, []],

        "bv 5000~8000 & evd -∞~1000": [0, []],
        "bv 5000~8000 & evd 1000~4200": [0, []],
        "bv 5000~8000 & evd 4200~6300": [0, []],
        "bv 5000~8000 & evd 6300+": [0, []],

        "bv 8000~10000 & evd -∞~1000": [0, []],
        "bv 8000~10000 & evd 1000~4200": [0, []],
        "bv 8000~10000 & evd 4200~6300": [0, []],
        "bv 8000~10000 & evd 6300+": [0, []],

        "bv 10000+ & evd -∞~1000": [0, []],
        "bv 10000+ & evd 1000~4200": [0, []],
        "bv 10000+ & evd 4200~6300": [0, []],
        "bv 10000+ & evd 6300+": [0, []],

        # 人脸
        "bv -∞~-2000 & fdr -∞~0": [0, []],
        "bv -∞~-2000 & fdr 0~1800": [0, []],
        "bv -∞~-2000 & fdr 1800~2800": [0, []],
        "bv -∞~-2000 & fdr 2800~4000": [0, []],
        "bv -∞~-2000 & fdr 4000+": [0, []],

        "bv -2000~0 & fdr -∞~0": [0, []],
        "bv -2000~0 & fdr 0~1800": [0, []],
        "bv -2000~0 & fdr 1800~2800": [0, []],
        "bv -2000~0 & fdr 2800~4000": [0, []],
        "bv -2000~0 & fdr 4000+": [0, []],

        "bv 0~3000 & fdr -∞~0": [0, []],
        "bv 0~3000 & fdr 0~1800": [0, []],
        "bv 0~3000 & fdr 1800~2800": [0, []],
        "bv 0~3000 & fdr 2800~4000": [0, []],
        "bv 0~3000 & fdr 4000+": [0, []],

        "bv 3000~6000 & fdr -∞~0": [0, []],
        "bv 3000~6000 & fdr 0~1800": [0, []],
        "bv 3000~6000 & fdr 1800~2800": [0, []],
        "bv 3000~6000 & fdr 2800~4000": [0, []],
        "bv 3000~6000 & fdr 4000+": [0, []],

        "bv 6000~8500 & fdr -∞~0": [0, []],
        "bv 6000~8500 & fdr 0~1800": [0, []],
        "bv 6000~8500 & fdr 1800~2800": [0, []],
        "bv 6000~8500 & fdr 2800~4000": [0, []],
        "bv 6000~8500 & fdr 4000+": [0, []],

        "bv 8500+ & fdr -∞~0": [0, []],
        "bv 8500+ & fdr 0~1800": [0, []],
        "bv 8500+ & fdr 1800~2800": [0, []],
        "bv 8500+ & fdr 2800~4000": [0, []],
        "bv 8500+ & fdr 4000+": [0, []],

    }

    with open(exifpath + "SearchResult.exif", 'r') as ef:
        lines = ef.readlines()
    count = 0
    # 拿到每一个exif中需要的信息 放在data中

    for line in lines:

        count += 1
        # 取每一张图片的信息 放在data中
        if count % 8 != 0:
            if ".jpg" in line:
                data["name"] = re.findall(r"[\[](.*?)[\]]", line)[0]
                name = photopath.split("/")[-2]
            elif line != "\n":
                dataname = line.split(":")[0].replace(" ", "")
                value = line.split(":")[1].replace(" ", "").replace("\n", "")
                data[dataname] = value
        else:
            # 判断每个exif的区域信息
            if int(data["AE_TAG_FACE_STS_SIZE"]) > 13 and int(data["AE_TAG_FLT_FDY"]) != 0:
                if 0 > int(data['AE_TAG_FLT_DR']) and -2000 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-2000 & fdr -∞~0'][0] += 1
                    result['bv -∞~-2000 & fdr -∞~0'][1].append(data["name"])
                if 1800 > int(data['AE_TAG_FLT_DR']) >= 0 and -2000 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-2000 & fdr 0~1800'][0] += 1
                    result['bv -∞~-2000 & fdr 0~1800'][1].append(data["name"])
                if 2800 > int(data['AE_TAG_FLT_DR']) >= 1800 and -2000 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-2000 & fdr 1800~2800'][0] += 1
                    result['bv -∞~-2000 & fdr 1800~2800'][1].append(data["name"])
                if 4000 > int(data['AE_TAG_FLT_DR']) >= 2800 and -2000 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-2000 & fdr 2800~4000'][0] += 1
                    result['bv -∞~-2000 & fdr 2800~4000'][1].append(data["name"])
                if int(data['AE_TAG_FLT_DR']) >= 4000 and -2000 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-2000 & fdr 4000+'][0] += 1
                    result['bv -∞~-2000 & fdr 4000+'][1].append(data["name"])

                if 0 > int(data['AE_TAG_FLT_DR']) and 0 > int(data['AE_TAG_REALBVX1000']) >= -2000:
                    result['bv -2000~0 & fdr -∞~0'][0] += 1
                    result['bv -2000~0 & fdr -∞~0'][1].append(data["name"])
                if 1800 > int(data['AE_TAG_FLT_DR']) >= 0 and 0 > int(data['AE_TAG_REALBVX1000']) >= -2000:
                    result['bv -2000~0 & fdr 0~1800'][0] += 1
                    result['bv -2000~0 & fdr 0~1800'][1].append(data["name"])
                if 2800 > int(data['AE_TAG_FLT_DR']) >= 1800 and 0 > int(data['AE_TAG_REALBVX1000']) >= -2000:
                    result['bv -2000~0 & fdr 1800~2800'][0] += 1
                    result['bv -2000~0 & fdr 1800~2800'][1].append(data["name"])
                if 4000 > int(data['AE_TAG_FLT_DR']) >= 2800 and 0 > int(data['AE_TAG_REALBVX1000']) >= -2000:
                    result['bv -2000~0 & fdr 2800~4000'][0] += 1
                    result['bv -2000~0 & fdr 2800~4000'][1].append(data["name"])
                if int(data['AE_TAG_FLT_DR']) >= 4000 and 0 > int(data['AE_TAG_REALBVX1000']) >= -2000:
                    result['bv -2000~0 & fdr 4000+'][0] += 1
                    result['bv -2000~0 & fdr 4000+'][1].append(data["name"])

                if 0 > int(data['AE_TAG_FLT_DR']) and 3000 > int(data['AE_TAG_REALBVX1000']) >= 0:
                    result['bv 0~3000 & fdr -∞~0'][0] += 1
                    result['bv 0~3000 & fdr -∞~0'][1].append(data["name"])
                if 1800 > int(data['AE_TAG_FLT_DR']) >= 0 and 3000 > int(data['AE_TAG_REALBVX1000']) >= 0:
                    result['bv 0~3000 & fdr 0~1800'][0] += 1
                    result['bv 0~3000 & fdr 0~1800'][1].append(data["name"])
                if 2800 > int(data['AE_TAG_FLT_DR']) >= 1800 and 3000 > int(data['AE_TAG_REALBVX1000']) >= 0:
                    result['bv 0~3000 & fdr 1800~2800'][0] += 1
                    result['bv 0~3000 & fdr 1800~2800'][1].append(data["name"])
                if 4000 > int(data['AE_TAG_FLT_DR']) >= 2800 and 3000 > int(data['AE_TAG_REALBVX1000']) >= 0:
                    result['bv 0~3000 & fdr 2800~4000'][0] += 1
                    result['bv 0~3000 & fdr 2800~4000'][1].append(data["name"])
                if int(data['AE_TAG_FLT_DR']) >= 4000 and 3000 > int(data['AE_TAG_REALBVX1000']) >= 0:
                    result['bv 0~3000 & fdr 4000+'][0] += 1
                    result['bv 0~3000 & fdr 4000+'][1].append(data["name"])

                if 0 > int(data['AE_TAG_FLT_DR']) and 6000 > int(data['AE_TAG_REALBVX1000']) >= 3000:
                    result['bv 3000~6000 & fdr -∞~0'][0] += 1
                    result['bv 3000~6000 & fdr -∞~0'][1].append(data["name"])
                if 1800 > int(data['AE_TAG_FLT_DR']) >= 0 and 6000 > int(data['AE_TAG_REALBVX1000']) >= 3000:
                    result['bv 3000~6000 & fdr 0~1800'][0] += 1
                    result['bv 3000~6000 & fdr 0~1800'][1].append(data["name"])
                if 2800 > int(data['AE_TAG_FLT_DR']) >= 1800 and 6000 > int(data['AE_TAG_REALBVX1000']) >= 3000:
                    result['bv 3000~6000 & fdr 1800~2800'][0] += 1
                    result['bv 3000~6000 & fdr 1800~2800'][1].append(data["name"])
                if 4000 > int(data['AE_TAG_FLT_DR']) >= 2800 and 6000 > int(data['AE_TAG_REALBVX1000']) >= 3000:
                    result['bv 3000~6000 & fdr 2800~4000'][0] += 1
                    result['bv 3000~6000 & fdr 2800~4000'][1].append(data["name"])
                if int(data['AE_TAG_FLT_DR']) >= 4000 and 6000 > int(data['AE_TAG_REALBVX1000']) >= 3000:
                    result['bv 3000~6000 & fdr 4000+'][0] += 1
                    result['bv 3000~6000 & fdr 4000+'][1].append(data["name"])

                if 0 > int(data['AE_TAG_FLT_DR']) and 8500 > int(data['AE_TAG_REALBVX1000']) >= 6000:
                    result['bv 6000~8500 & fdr -∞~0'][0] += 1
                    result['bv 6000~8500 & fdr -∞~0'][1].append(data["name"])
                if 1800 > int(data['AE_TAG_FLT_DR']) >= 0 and 8500 > int(data['AE_TAG_REALBVX1000']) >= 6000:
                    result['bv 6000~8500 & fdr 0~1800'][0] += 1
                    result['bv 6000~8500 & fdr 0~1800'][1].append(data["name"])
                if 2800 > int(data['AE_TAG_FLT_DR']) >= 1800 and 8500 > int(data['AE_TAG_REALBVX1000']) >= 6000:
                    result['bv 6000~8500 & fdr 1800~2800'][0] += 1
                    result['bv 6000~8500 & fdr 1800~2800'][1].append(data["name"])
                if 4000 > int(data['AE_TAG_FLT_DR']) >= 2800 and 8500 > int(data['AE_TAG_REALBVX1000']) >= 6000:
                    result['bv 6000~8500 & fdr 2800~4000'][0] += 1
                    result['bv 6000~8500 & fdr 2800~4000'][1].append(data["name"])
                if int(data['AE_TAG_FLT_DR']) >= 4000 and 8500 > int(data['AE_TAG_REALBVX1000']) >= 6000:
                    result['bv 6000~8500 & fdr 4000+'][0] += 1
                    result['bv 6000~8500 & fdr 4000+'][1].append(data["name"])

                if 0 > int(data['AE_TAG_FLT_DR']) and int(data['AE_TAG_REALBVX1000']) >= 8500:
                    result['bv 8500+ & fdr -∞~0'][0] += 1
                    result['bv 8500+ & fdr -∞~0'][1].append(data["name"])
                if 1800 > int(data['AE_TAG_FLT_DR']) >= 0 and int(data['AE_TAG_REALBVX1000']) >= 8500:
                    result['bv 8500+ & fdr 0~1800'][0] += 1
                    result['bv 8500+ & fdr 0~1800'][1].append(data["name"])
                if 2800 > int(data['AE_TAG_FLT_DR']) >= 1800 and int(data['AE_TAG_REALBVX1000']) >= 8500:
                    result['bv 8500+ & fdr 1800~2800'][0] += 1
                    result['bv 8500+ & fdr 1800~2800'][1].append(data["name"])
                if 4000 > int(data['AE_TAG_FLT_DR']) >= 2800 and int(data['AE_TAG_REALBVX1000']) >= 8500:
                    result['bv 8500+ & fdr 2800~4000'][0] += 1
                    result['bv 8500+ & fdr 2800~4000'][1].append(data["name"])
                if int(data['AE_TAG_FLT_DR']) >= 4000 and int(data['AE_TAG_REALBVX1000']) >= 8500:
                    result['bv 8500+ & fdr 4000+'][0] += 1
                    result['bv 8500+ & fdr 4000+'][1].append(data["name"])

            else:
                if 1000 > int(data['AE_TAG_HS_EVD']) and -1500 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-1500 & evd -∞~1000'][0] += 1
                    result['bv -∞~-1500 & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and -1500 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-1500 & evd 1000~4200'][0] += 1
                    result['bv -∞~-1500 & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and -1500 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-1500 & evd 4200~6300'][0] += 1
                    result['bv -∞~-1500 & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and -1500 > int(data['AE_TAG_REALBVX1000']):
                    result['bv -∞~-1500 & evd 6300+'][0] += 1
                    result['bv -∞~-1500 & evd 6300+'][1].append(data["name"])

                if 1000 > int(data['AE_TAG_HS_EVD']) and 500 > int(data['AE_TAG_REALBVX1000']) >= -1500:
                    result['bv -1500~500 & evd -∞~1000'][0] += 1
                    result['bv -1500~500 & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and 500 > int(data['AE_TAG_REALBVX1000']) >= -1500:
                    result['bv -1500~500 & evd 1000~4200'][0] += 1
                    result['bv -1500~500 & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and 500 > int(data['AE_TAG_REALBVX1000']) >= -1500:
                    result['bv -1500~500 & evd 4200~6300'][0] += 1
                    result['bv -1500~500 & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and 500 > int(data['AE_TAG_REALBVX1000']) >= -1500:
                    result['bv -1500~500 & evd 6300+'][0] += 1
                    result['bv -1500~500 & evd 6300+'][1].append(data["name"])

                if 1000 > int(data['AE_TAG_HS_EVD']) and 2500 > int(data['AE_TAG_REALBVX1000']) >= 500:
                    result['bv 500~2500 & evd -∞~1000'][0] += 1
                    result['bv 500~2500 & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and 2500 > int(data['AE_TAG_REALBVX1000']) >= 500:
                    result['bv 500~2500 & evd 1000~4200'][0] += 1
                    result['bv 500~2500 & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and 2500 > int(data['AE_TAG_REALBVX1000']) >= 500:
                    result['bv 500~2500 & evd 4200~6300'][0] += 1
                    result['bv 500~2500 & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and 2500 > int(data['AE_TAG_REALBVX1000']) >= 500:
                    result['bv 500~2500 & evd 6300+'][0] += 1
                    result['bv 500~2500 & evd 6300+'][1].append(data["name"])

                if 1000 > int(data['AE_TAG_HS_EVD']) and 5000 > int(data['AE_TAG_REALBVX1000']) >= 2500:
                    result['bv 2500~5000 & evd -∞~1000'][0] += 1
                    result['bv 2500~5000 & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and 5000 > int(data['AE_TAG_REALBVX1000']) >= 2500:
                    result['bv 2500~5000 & evd 1000~4200'][0] += 1
                    result['bv 2500~5000 & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and 5000 > int(data['AE_TAG_REALBVX1000']) >= 2500:
                    result['bv 2500~5000 & evd 4200~6300'][0] += 1
                    result['bv 2500~5000 & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and 5000 > int(data['AE_TAG_REALBVX1000']) >= 2500:
                    result['bv 2500~5000 & evd 6300+'][0] += 1
                    result['bv 2500~5000 & evd 6300+'][1].append(data["name"])

                if 1000 > int(data['AE_TAG_HS_EVD']) and 8000 > int(data['AE_TAG_REALBVX1000']) >= 5000:
                    result['bv 5000~8000 & evd -∞~1000'][0] += 1
                    result['bv 5000~8000 & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and 8000 > int(data['AE_TAG_REALBVX1000']) >= 5000:
                    result['bv 5000~8000 & evd 1000~4200'][0] += 1
                    result['bv 5000~8000 & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and 8000 > int(data['AE_TAG_REALBVX1000']) >= 5000:
                    result['bv 5000~8000 & evd 4200~6300'][0] += 1
                    result['bv 5000~8000 & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and 8000 > int(data['AE_TAG_REALBVX1000']) >= 5000:
                    result['bv 5000~8000 & evd 6300+'][0] += 1
                    result['bv 5000~8000 & evd 6300+'][1].append(data["name"])

                if 1000 > int(data['AE_TAG_HS_EVD']) and 10000 > int(data['AE_TAG_REALBVX1000']) >= 8000:
                    result['bv 8000~10000 & evd -∞~1000'][0] += 1
                    result['bv 8000~10000 & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and 10000 > int(data['AE_TAG_REALBVX1000']) >= 8000:
                    result['bv 8000~10000 & evd 1000~4200'][0] += 1
                    result['bv 8000~10000 & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and 10000 > int(data['AE_TAG_REALBVX1000']) >= 8000:
                    result['bv 8000~10000 & evd 4200~6300'][0] += 1
                    result['bv 8000~10000 & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and 10000 > int(data['AE_TAG_REALBVX1000']) >= 8000:
                    result['bv 8000~10000 & evd 6300+'][0] += 1
                    result['bv 8000~10000 & evd 6300+'][1].append(data["name"])

                if 1000 > int(data['AE_TAG_HS_EVD']) and int(data['AE_TAG_REALBVX1000']) >= 10000:
                    result['bv 10000+ & evd -∞~1000'][0] += 1
                    result['bv 10000+ & evd -∞~1000'][1].append(data["name"])
                if 4200 > int(data['AE_TAG_HS_EVD']) >= 1000 and int(data['AE_TAG_REALBVX1000']) >= 10000:
                    result['bv 10000+ & evd 1000~4200'][0] += 1
                    result['bv 10000+ & evd 1000~4200'][1].append(data["name"])
                if 6300 > int(data['AE_TAG_HS_EVD']) >= 4200 and int(data['AE_TAG_REALBVX1000']) >= 10000:
                    result['bv 10000+ & evd 4200~6300'][0] += 1
                    result['bv 10000+ & evd 4200~6300'][1].append(data["name"])
                if int(data['AE_TAG_HS_EVD']) >= 6300 and int(data['AE_TAG_REALBVX1000']) >= 10000:
                    result['bv 10000+ & evd 6300+'][0] += 1
                    result['bv 10000+ & evd 6300+'][1].append(data["name"])

            # 把这一个exif放数组中
            if int(data['AE_TAG_FACE_STS_SIZE']) > 13 and int(data["AE_TAG_FLT_FDY"]) != 0:
                AE_TAG_REALBVX1000.append(data['AE_TAG_REALBVX1000'])
                AE_TAG_FLT_DR.append(data['AE_TAG_FLT_DR'])
            else:
                AE_TAG_REALBVX1000_Normal.append(data['AE_TAG_REALBVX1000'])
                AE_TAG_HS_EVD_Normal.append(data['AE_TAG_HS_EVD'])



    return result, name, AE_TAG_HS_EVD_Normal, AE_TAG_REALBVX1000_Normal, AE_TAG_REALBVX1000,AE_TAG_FLT_DR,


def run_AE():
    global photopath
    result_CCT = [ "CCT  -∞  ~  2300", "CCT =2300 ~ 2850", "CCT =2850 ~ 3750",
                   "CCT =3750 ~ 5100", "CCT =5100 ~ 6500", "CCT =6500 ~   ∞"]
    for cct_key in result_CCT:
        dir_name = photopath + "\\" + cct_key
        if os.path.isdir(dir_name):
            shutil.rmtree(dir_name)
    result_AE=["Face","Normal"]
    for cct_key in result_AE:
        dir_name = photopath + "\\" + cct_key
        if os.path.isdir(dir_name):
            shutil.rmtree(dir_name)
    # child = subprocess.Popen(f"{debug} -dump {photopath}  -find \"AE_TAG_HS_EVD|AE_TAG_REALBVX1000|AE_TAG_FACE_STS_SIZE|AE_TAG_FLT_DR\" -save {photopath}/exif/")
    child1 = subprocess.Popen(
        f"{debug_Tool} -dump {photopath} -find \"AE_TAG_HS_EVD|AE_TAG_REALBVX1000|AE_TAG_FACE_STS_SIZE|AE_TAG_FLT_DR|AE_TAG_FLT_FDY\" -save {photopath}/")

    child1.wait()

    result,name, AE_TAG_HS_EVD_Normal, AE_TAG_REALBVX1000_Normal, AE_TAG_REALBVX1000,AE_TAG_FLT_DR, = analyse(photopath)


    os.mkdir(photopath + "Face")
    os.mkdir(photopath + "Normal")

    for dirname in result:
        if not os.path.exists(str(result[dirname][0]) + "  " + dirname.replace(" ", " ")):
            os.mkdir(photopath + str(result[dirname][0]) + "  " + dirname.replace(" ", " "))

    for photoname in os.listdir(photopath):
        for dirname in result:
            if photoname in result[dirname][1]:
                shutil.copyfile(photopath + photoname,
                                photopath + str(result[dirname][0]) + "  " + dirname.replace(" ",
                                                                                             " ") + f"/{photoname}")

    # 帮助跳出外层循环
    switchflag = True
    for dirpath, dirnames, filenames in os.walk(photopath):
        if switchflag:
            break
    for dirname in dirnames:
        if dirname == "Face" or dirname == "Normal":
            switchflag = False
            break
        if "evd" in dirname:
            shutil.move(photopath + dirname, photopath + "Normal")
        if "fdr" in dirname:
            shutil.move(photopath + dirname, photopath + "Face")

    x = len(AE_TAG_FLT_DR)
    y = len(AE_TAG_HS_EVD_Normal)
    result1 = '{:.2%}'.format(x / (x + y))
    result2 = '{:.2%}'.format(y / (x + y))

    ##绘制Normal散点图表
    x_bv = np.asarray(AE_TAG_REALBVX1000_Normal, dtype=int)
    y_evd = np.asarray(AE_TAG_HS_EVD_Normal, dtype=int)


    my_x_ticks = [-1500, 500, 2500, 5000, 8000, 10000]
    my_y_ticks = [0, 1000, 4200, 6300]
    plt.xlim(-8000, 15000)
    plt.ylim(-5000, 10000)
    plt.xticks(ticks=my_x_ticks)
    plt.yticks(ticks=my_y_ticks)
    # 设置图表标题并给坐标轴加上标签
    plt.title(f'{name} Normal:{y}/{x + y}  {result2}', fontsize=24)
    plt.xlabel('AE_TAG_REALBVX1000', fontsize=14)
    plt.ylabel('AE_TAG_HS_EVD', fontsize=14)
    plt.scatter(x_bv, y_evd, s=30,label="pic")

    plt.legend()
    plt.savefig(f'{photopath}Normal/result_Normal.png', dpi=300)
    plt.close()


    ##绘制Face散点图
    x_bv = np.asarray(AE_TAG_REALBVX1000, dtype=int)
    y_dr = np.asarray(AE_TAG_FLT_DR, dtype=int)

    my_x_ticks = [-2000, 0, 3000, 6000, 8500]
    my_y_ticks = [0, 1800, 2800, 4000]
    plt.xlim(-8000, 15000)
    plt.ylim(-2000, 8000)
    plt.xticks(my_x_ticks)
    plt.yticks(my_y_ticks)
    # 设置图表标题并给坐标轴加上标签

    plt.title(f'{name} Face:{x}/{x + y}  {result1}', fontsize=24)
    plt.xlabel('AE_TAG_REALBVX1000', fontsize=14)
    plt.ylabel('AE_TAG_FLT_DR', fontsize=14)
    plt.scatter(x_bv, y_dr, s=20, label="pic")

    plt.legend()
    plt.savefig(f'{photopath}Face/result_Face.png', dpi=300)
    plt.close()
    endtime = datetime.datetime.now()

    showinfo(title='提示', message='AE分类 操作成功!')

class result_LV_obj:
    def __init__(self):
        self.result_LV = {
            "LV  -∞  ~  0" : 0,
            'LV =0 ~ 20'   : 0,
            'LV =20 ~ 40'  : 0,
            'LV =40 ~ 60'  : 0,
            'LV =60 ~ 80'  : 0,
            'LV =80 ~ 100' : 0,
            'LV =100 ~ 120': 0,
            'LV =120 ~ 140': 0,
            'LV =140 ~ 160': 0,
            'LV =160 ~ 180': 0,
        }

def analyse_AWB(exifpath,result_CCT):
    with open(exifpath + "\SearchResult.exif", 'r', encoding='utf-8') as ef:
        lines = ef.readlines()

    cur_file_name = ''
    cur_file_path = ''
    cur_CCT_key = ''
    cur_LV_key = ''
    for line in lines:
        cur_line_is_CCT = 0
        cur_line_is_LV = 0
        if ".jpg" in line:
            cur_file_path = exifpath + '\\'
            cur_file_name = re.findall(r"[\[](.*?)[\]]", line)[0]
            cur_CCT_key = ''
            cur_LV_key = ''
            continue
        elif line != "\n":
            data_name = line.split(":")[0].replace(" ", "")
            data_value = line.split(":")[1].replace(" ", "").replace("\n", "")
            if data_name == 'AWB_TAG_CCT':
                cur_line_is_CCT = 1
            elif data_name == 'AWB_TAG_ALGO_SCENE_LV':
                cur_line_is_LV = 1
            else:
                continue
        if cur_line_is_CCT == 1:
            cur_CCT = int(data_value)
            if cur_CCT < 2300:
                cur_CCT_key = 'CCT  -∞  ~  2300'
            elif cur_CCT < 2850:
                cur_CCT_key = 'CCT =2300 ~ 2850'
            elif cur_CCT < 3750:
                cur_CCT_key = 'CCT =2850 ~ 3750'
            elif cur_CCT < 5100:
                cur_CCT_key = 'CCT =3750 ~ 5100'
            elif cur_CCT < 6500:
                cur_CCT_key = 'CCT =5100 ~ 6500'
            else:
                cur_CCT_key = 'CCT =6500 ~   ∞'
            if cur_LV_key != '':
                if not os.path.exists(cur_file_path+ cur_CCT_key + "/" + cur_LV_key):
                    if not os.path.exists(cur_file_path+ cur_CCT_key):
                        os.mkdir(cur_file_path+ cur_CCT_key )
                    os.mkdir(cur_file_path + cur_CCT_key + "/" + cur_LV_key)
                result_CCT[cur_CCT_key].result_LV[cur_LV_key] += 1
                src_path = cur_file_path + cur_file_name
                dst_path = cur_file_path + cur_CCT_key + "\\" + cur_LV_key +"\\"+cur_file_name
                with open(src_path, 'rb') as f_src:
                    data = f_src.read()
                    with open(dst_path, 'wb') as f_dst:
                        f_dst.write(data)
                        f_dst.close()
                    f_src.close()
                cur_CCT_key = ''
                cur_LV_key = ''
                cur_file_name = ''
        elif  cur_line_is_LV == 1:
            cur_LV = int(data_value)
            if cur_LV < 0:
                cur_LV_key = 'LV  -∞  ~  0'
            elif cur_LV < 20:
                cur_LV_key = 'LV =0 ~ 20'
            elif cur_LV < 40:
                cur_LV_key = 'LV =20 ~ 40'
            elif cur_LV < 60:
                cur_LV_key = 'LV =40 ~ 60'
            elif cur_LV < 80:
                cur_LV_key = 'LV =60 ~ 80'
            elif cur_LV < 100:
                cur_LV_key = 'LV =80 ~ 100'
            elif cur_LV < 120:
                cur_LV_key = 'LV =100 ~ 120'
            elif cur_LV < 140:
                cur_LV_key = 'LV =120 ~ 140'
            elif cur_LV < 160:
                cur_LV_key = 'LV =140 ~ 160'
            elif cur_LV < 180:
                cur_LV_key = 'LV =160 ~ 180'


def run_AWB():
    result_LV_0 = result_LV_obj()
    result_LV_1 = result_LV_obj()
    result_LV_2 = result_LV_obj()
    result_LV_3 = result_LV_obj()
    result_LV_4 = result_LV_obj()
    result_LV_5 = result_LV_obj()
    result_CCT = {
        "CCT  -∞  ~  2300": result_LV_0,
        "CCT =2300 ~ 2850": result_LV_1,
        "CCT =2850 ~ 3750": result_LV_2,
        "CCT =3750 ~ 5100": result_LV_3,
        "CCT =5100 ~ 6500": result_LV_4,
        "CCT =6500 ~   ∞": result_LV_5,
    }
    global photopath
    for cct_key in result_CCT:
        dir_name = photopath + "\\" + cct_key
        if os.path.isdir(dir_name):
            shutil.rmtree(dir_name)
    result_AE=["Face","Normal"]
    for cct_key in result_AE:
        dir_name = photopath + "\\" + cct_key
        if os.path.isdir(dir_name):
            shutil.rmtree(dir_name)
    child1_AWB = subprocess.Popen(
        f"{debug_Tool} -dump {photopath} -find \"AWB_TAG_CCT|AWB_TAG_ALGO_SCENE_LV\" -save {photopath}/")
    child1_AWB.wait()

    analyse_AWB(photopath,result_CCT)
    for cct_key in result_CCT:
        for lv_key in result_CCT[cct_key].result_LV:
            lv_count = result_CCT[cct_key].result_LV[lv_key]
            if os.path.exists(photopath+'\\'+ cct_key +'\\'+ lv_key) == False:
                if os.path.exists(photopath+'\\'+ cct_key) == False:
                    os.mkdir(photopath + '\\' + cct_key )
                os.mkdir(photopath+'\\'+ cct_key +'\\'+ lv_key)
            os.rename(photopath+'\\'+ cct_key +'\\'+ lv_key,
                    photopath+'\\'+ cct_key +'\\'+ '_'+str(lv_count)+'_'+lv_key)
    showinfo(title='提示', message='AWB分类 操作成功!')

# 初始化布局
root = tk.Tk()
root.title("AE_AWB图片分类小工具")

img_use_pic = open(r'C:PCBico.png', 'wb')  # 创建临时使用图片文件,这里放在了C盘的temp文件夹里
img_use_pic.write(base64.b64decode(img_pic))
img_use_pic.close()
image = tk.PhotoImage(file='C:PCBico.png')
root.iconphoto(False,image)
root.wm_iconphoto(False,image)
max_w, max_h = root.maxsize()
root.geometry(f'500x300+{int((max_w - 500) / 2)}+{int((max_h - 300) / 2)}')  # 居中
root.resizable(width=False, height=False)

# 初始化Entry控件
debug_entry = tk.StringVar()  # 选择目录信息
photo_entry = tk.StringVar()  # 输出路径信息

if os.path.exists(os.getcwd() + "\default_path.txt"):
    with open(os.getcwd() + "\default_path.txt", 'r') as f_default_path:
        lines = f_default_path.readlines()
        print(lines)
        for line in lines:
            if line.startswith("tool_deault_dir=") :
                default_tool_dir = line.split("tool_deault_dir=")[1].replace("\n", "")
                print(default_tool_dir)
                print(default_tool_dir)
                if "DebugParser.exe" in default_tool_dir:
                    debug_entry.set(default_tool_dir)
                    debug_Tool = default_tool_dir
            if line.startswith("photo_deault_dir=") :
                default_photo_dir = line.split("photo_deault_dir=")[1].replace("\n", "")
                if os.path.isdir(default_photo_dir):
                    photo_entry.set(default_photo_dir)
                    photopath = default_photo_dir


# === 布局控件
# raw路径 label
label = tk.Label(root, text="DP路径", font=("", 15))
label.place(x=50, y=80)

# 输入路径
raw_path = tk.Entry(root, textvariable=debug_entry, font=('', 15), width=20)
raw_path.place(x=130, y=80)

# 选择文件夹 按钮
button_raw = tk.Button(root, text="选择文件", command=select_tool_dir, font=('', 10), width=15)
button_raw.place(x=350, y=80)

# out路径 label
label2 = tk.Label(root, text="Pic路径", font=("", 15))
label2.place(x=50, y=160)

# out信息框
out_path = tk.Entry(root, textvariable=photo_entry, font=('', 15), width=20)
out_path.place(x=130, y=160)

button_raw = tk.Button(root, text="选择文件夹", command=select_picture_path, font=('', 10), width=15)
button_raw.place(x=350, y=160)


button_raw = tk.Button(root, text="运行AE分类", command=run_AE, font=('', 10), width=15)
button_raw.place(x=80, y=220)

button_raw = tk.Button(root, text="运行AWB分类", command=run_AWB, font=('', 10), width=15)
button_raw.place(x=240, y=220)
root.mainloop()
os.remove(r'C:PCBico.png')

要点1:Popen 调用windows工具,命令行可执行的命令

 subprocess.Popen(
        f"{debug_Tool} -dump {photopath} -find \"AWB_TAG_CCT|AWB_TAG_ALGO_SCENE_LV\" -save {photopath}/")

要点2:将图片文件转换为 py 文件,从代码中调用,方便打包

from png_resource import img as img_pic

root = tk.Tk()
img_use_pic = open(r'C:PCBico.png', 'wb')  # 创建临时使用图片文件,这里放在了C盘的temp文件夹里
img_use_pic.write(base64.b64decode(img_pic))
img_use_pic.close()
image = tk.PhotoImage(file='C:PCBico.png')
root.iconphoto(False,image)
root.wm_iconphoto(False,image)


os.remove(r'C:PCBico.png')  #结束时调用

要点3:python 用class 模拟C 语音的结构体,字典嵌套实现如下,可以都放入class 内部简化代码

class result_LV_obj:
    def __init__(self):
        self.result_LV = {
            "LV  -∞  ~  0" : 0,
            'LV =0 ~ 20'   : 0,
            'LV =20 ~ 40'  : 0,
            'LV =40 ~ 60'  : 0,
            'LV =60 ~ 80'  : 0,
            'LV =80 ~ 100' : 0,
            'LV =100 ~ 120': 0,
            'LV =120 ~ 140': 0,
            'LV =140 ~ 160': 0,
            'LV =160 ~ 180': 0,
        }
def run_AWB():
    result_LV_0 = result_LV_obj()
    result_LV_1 = result_LV_obj()
    result_LV_2 = result_LV_obj()
    result_LV_3 = result_LV_obj()
    result_LV_4 = result_LV_obj()
    result_LV_5 = result_LV_obj()
    result_CCT = {
        "CCT  -∞  ~  2300": result_LV_0,
        "CCT =2300 ~ 2850": result_LV_1,
        "CCT =2850 ~ 3750": result_LV_2,
        "CCT =3750 ~ 5100": result_LV_3,
        "CCT =5100 ~ 6500": result_LV_4,
        "CCT =6500 ~   ∞": result_LV_5,
    }

未解决问题:

         1. 原本采用 递归 遍历文件夹 的方式,每完成对当前文件的分类时直接修改目标文件夹名称;总是会提示文件夹仍然被占用错误。

         2. 后尝试递归函数放入单独的线程中执行,子线程遍历过程中维护2个列表,分别保存需要修改的文件名完整路径 和 重命名后的文件名完整路径,并在这个子线程结束调用后再遍历列表,对文件夹进行重命名,仍然有相同文件仍然被占用错误。主线程中在子线程结束后仍然加2秒延时,还是会有这个错误。。

        3. 尝试列表中直接写死直接对文件夹调用重命名,可以成功。。。未知文件被占用的根本原因。

        4. 文件被占用查询占用情况详细信息方法未尝试成功,待继续分析。

你可能感兴趣的:(python,开发语言)