pyinstaller包括一个gif

时间:2017-02-05 00:22:35

标签: python pyinstaller

Hello world我试图在我的单个.exe中添加一个gif,我不必让它们在同一个位置打开带有gif的exe。我试图将gif包含在程序中,这样他们就可以在任何他们想要的地方打开程序,并且gif仍然在程序中。

这是我粘贴到我的cmd中创建单个

的内容
C:\Python27\Scripts\pyinstaller.exe --onefile --windowed --icon=fav.ico Program.pyw

还有另一个 - 我可以做的命令包括gif吗?例如:

--includeFile=MYgif.gif

先谢谢

2 个答案:

答案 0 :(得分:0)

我强烈建议您使用 base64.b64encode() 将gif转换为字符串并将其嵌入py文件,然后只需import此文件您的主脚本pyinstaller会自动将此文件打包到您的exe中。

import base64

# Encode your gif into a string
img_encoded = base64.b64encode(open('example.gif', 'rb').read()) 
# It looks like this: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcG ......

# Then embed this string into mygif.py, 
# and in your main script: import mygif

# Decode the image into binary content
img = base64.b64decode(img_encoded)

如果图片是jpeg或png,并且您恰好使用wxPython进行GUI,那么使用它也非常方便img2py。但是我假设您的gif文件是动画的,那么您可能必须对每个帧进行编码,以便在使用img2py时保留所有图像数据。

答案 1 :(得分:0)

您需要编辑pyinstaller生成的spec文件并将图像添加到其中。

使用pyinstaller时,它会输出一个.spec文件,这是一个配置文件。如果你想使用相同的选项再次构建你的程序,你可以使用' pyinstaller myprogram.spec'而不是' pyinstaller myprogram.py'

因此,在文本编辑器中打开spec文件,并将图像放入'数据'像这样的名单。

datas=[('my_image1.gif', ''),
       ('my_image2.gif', ''),
       ('my_image3.gif', ''),
       ('my_image4.gif', '')],

在执行使用pyinstaller创建的onefile程序期间,数据文件将解压缩到临时目录。因此,要从程序中访问您的img文件,请使用这样的函数。

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    (return os.path.join(base_path, relative_path)

我从此链接Bundling data files with PyInstaller (--onefile)

中抓取了该功能

现在使用您编辑的spec文件构建程序。 ' pyinstaller myProgram.spec'