Python exe无法识别图像文件

Python exe cannot identify image file

我正在制作一个 Tkinter 应用程序并使用 Pyinstaller 创建了我的项目的可执行文件,其中包含 main.py 文件和位于 "path/to/python/project/folder" 中的 2 个其他辅助 .py 文件:

pyinstaller --exclude-module PyQt5 --onefile -p "path/to/python/project/folder" main.py

在 main.py 文件中的某个位置,图像由用户从他们的系统 select 编辑,然后显示在 GUI 中。为此,我在 "path/to/python/project/folder" 中创建了 image.jpg。当 运行 在 IDE 中使用我的代码时,一切正常,但是当我 运行 main.exe 时,我收到以下错误:OSError: cannot identify image file 'image.jpg'

在 .exe 文件所在的同一文件夹中还创建了一个空图像。

有没有办法让 .exe 文件的行为与原始文件一样 python 'project'?或者可以不创建新文件并从 .exe 文件访问它们吗?

编辑:用户 select 的视频和应用程序将中间帧显示为图像,这就是创建新图像的原因。

编辑:这里有一些代码可以澄清一些事情:

import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog
import cv2
from PIL import Image, ImageTk
import os


fpsGlobal = -1

class GUI:
    def __init__(self, master):
        self.master = master
        self.master.title("TITLE")
        self.readyForAnalysis = False
        self.workdir = os.getcwd()
        self.data = None
        self.fps = fpsGlobal
        self.finished = False

        self.frame = tk.Frame(master)
        self.frame.pack()

        self.menu = tk.Menu(self.frame)
        self.file_expand = tk.Menu(self.menu, tearoff=0)
        self.file_expand.add_command(label='Open...',command=self.openVideo)
        self.menu.add_cascade(label='File', menu=self.file_expand)
        self.master.config(menu=self.menu)


    def openVideo(self):
        '''Opens the video when open... button is clicked and shows a screenshot of a frame from the video'''
        self.filename = filedialog.askopenfilename(initialdir = '/', title = 'Select file', filetypes = (("avi files",".avi"),("all files","*.*")))

        # if a video is loaded and openfiledialog is not cancelled
        if self.filename:
            # read videofile
            cap = cv2.VideoCapture(self.filename)
            self.totalFrames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            global fpsGlobal 
            fpsGlobal = int(cap.get(cv2.CAP_PROP_FPS))
            cap.set(cv2.CAP_PROP_POS_FRAMES, int(self.totalFrames/2))
            ret,frame = cap.read()
            cv2.imwrite("image.jpg", frame)

            # show image
            image = Image.open("image.jpg")
            #print("image.size   = (%d, %d)" % image.size)
            resizedImg = image.resize((704, 576), Image.ANTIALIAS)
            picture = ImageTk.PhotoImage(resizedImg)
            self.label = tk.Label(self.frame,image=picture)
            self.label.image = picture
            self.label.pack()

            cap.release()
            cv2.destroyAllWindows()

            try: 
                os.remove("image.jpg")
            except: print("no")

            self.readyForAnalysis = True
            self.analyzeButton.configure(background='green2')
            self.welcome.config(text="Start the analysis of the video by clicking the 'Analyze' button." )


def main():
    root = tk.Tk()
    root.geometry('1000x750')
    my_gui = GUI(root)
    root.mainloop()


if __name__== "__main__":
    main()

因此在 GUI 中,用户可以 select 稍后将分析的视频文件。为了向用户提供一些反馈,我将在 GUI 中将视频的中间帧显示为图像。当 运行 在我的 IDE 中使用代码时,一切正常,但是 运行 从 .exe 文件中使用它时,我在第 image = Image.open("image.jpg")[=15 行收到错误(参见上面的错误) =]

我相信我可能已经找到了解决我自己问题的方法。无法像在 Python 项目文件夹中那样在可执行文件中创建文件并在之后访问它们,因为 .exe 文件中没有文件夹结构。

解决这个问题的方法可能是使用 tempfile 库。