在 python 中导入图像,如果我想将程序文件夹移动到 运行,我的程序将无法工作,我必须更改我的代码

import image in python, my program cant work if i move program folder in different place if I want it to run, I have to change my code

我的程序有问题,我导入图像,它可以工作,但在成功的背后,我缺少了一些东西。 查看以下代码片段:

# python 3
from PIL import Image
from PIL import ImageTk

pathdesign_img = "D:/Python/Project1/image/design/

self.logo_Tbar_img = ImageTk.PhotoImage(Image.open(pathdesign_img+'Mlogo.jpg'))
self.logo_Tbar = tk.Label(self.bar_Tbar, image=self.logo_Tbar_img, bg="#DA291C")
self.logo_Tbar.pack(side=tk.LEFT, fill=tk.BOTH)

代码有效,但是当我想将所有程序文件夹移动到不同的地方时示例:C:\User\。我的程序不能 运行,如果我想要它 运行,我必须更改 pathdesign_img = "C:/User/...."。是否有任何我需要更改或添加的代码,以便我的程序可以 运行 在任何文件夹中,而无需更改 pathdesign_img

是的,应该可以。我假设您的 python-file 或 jupyter-notebook 位于“D:/Python/Project1”文件夹中,而您的图像位于“D:/Python/Project1/image/design”文件夹中。那么你可以这样做:

pathdesign_img = "image/design/"

简而言之,它的作用是:在您已经所在的文件夹中,它会搜索名为“image”的文件夹,并在该文件夹中搜索名为“design”的子文件夹。

是的,相对于您的应用程序目录的路径是正确的解决方案,但在将它们向前传递以供使用时,始终建议使用绝对路径。 因此,您使用相对于应用程序目录的绝对路径。

它可以很简单:

import os

dirpath   = os.path.abspath("images")
# os.path.abspath() will return the absolute path of the directory images
# that resides in the current working directory which you can discover with
# os.getcwd() and manipulate using os.chdir()
# Note that the current working directory may be manipulated from the OS.
# In the Windows's case you can specify it under properties of the shortcut
# and/or the executable. By the default it is the same directory where
# the executable lives, in this case, your script.
imagepath = os.path.join(dirpath, "<some_image>.jpg")

这是应该做的。它是跨平台的,很好的实践,不会给你带来路径上的麻烦。

但是,如果您打算将您的应用捆绑到例如EXE,你需要比这更狡猾。这是因为 *.exe 捆绑应用程序实际上是一个 ZIP 文件,添加了一些内容。 在这种情况下,图像路径将如下所示:

>>> print (imagepath)
C:\Program Files\Yours_app_folder\yourapp.exe\images\your_image.jpg

这当然是 OS 的无效路径。在这些情况下,我会执行以下操作。 我创建了一个名为 whereiam 或 whereami 的模块(名称取决于我的猜测),我在其中放置了一个 var 或函数,使我能够获得我的应用程序目录的正确路径。例如

import os, sys

def root ():
    # If the app is only a script, this will result in mydir
    # being a path to the directory where the 'whereiam' module lives:
    me = os.path.abspath(__file__)
    mydir = os.path.dirname(me)
    # If the app is bundled then sys.executable points to it instead
    # of Python interpreter, so first check for that
    if mydir.startswith(sys.executable):
        # Then we first get the directory where our app lives:
        mydir = os.path.dirname(sys.executable)
        # And if our whereiam is in a subdirectory, we find it by excluding
        # the executable's filename from the path:
        # by appending the rest to the mydir
        l = len(sys.executable)
        mydir = os.path.join(mydir.rstrip(os.sep), me[l:].lstrip(os.sep))
    return mydir

然后在您的主模块中,您只需执行以下操作:

import whereiam
import os

dirpath   = whereiam.root()
images    = os.path.join(dirpath, "images")
imagepath = os.path.join(images, "<your_image>.jpg")
# And you can then open imagepath with PIL or whatever being sure that it will be found if it is there