【问题标题】:Python exe cannot identify image filePython exe无法识别图像文件
【发布时间】:2019-07-25 15:01:28
【问题描述】:

我正在制作一个 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 文件中的某个位置,用户从他们的系统中选择了一个图像,然后在 GUI 中显示。为此,我在“path/to/python/project/folder”中创建了 image.jpg。在 IDE 中运行我的代码时一切正常,但是当我运行 main.exe 时出现以下错误:OSError: cannot identify image file 'image.jpg'

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

有没有办法让 .exe 文件表现得像原来的 python '项目'?还是不能简单地不创建新文件并从 .exe 文件访问它们?

编辑:用户选择一个视频,应用程序将 middel 帧显示为图像,这就是创建新图像的原因。

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

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 中,用户可以选择稍后将分析的视频文件。为了向用户提供一些反馈,我将在 GUI 中将视频的中间帧显示为图像。在我的 IDE 中运行代码时,一切正常,但从 .exe 文件运行时,我在 image = Image.open("image.jpg") 行出现错误(请参阅上面的错误)

【问题讨论】:

  • 我不太明白您要达到的目标,但您能否添加代码的最小工作版本来提问?
  • 我添加了一些代码和额外的解释希望这有助于你理解问题

标签: python tkinter exe pyinstaller


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 1970-01-01
    • 2020-12-17
    • 2020-05-26
    • 2017-09-11
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多