簡體   English   中英

pyinstaller包括一個gif

[英]pyinstaller include a gif

你好世界我想在我的單個.exe中添加一個gif,我不必讓它們在同一個位置打開帶有gif的exe。 我試圖將gif包含在程序中,這樣他們就可以在任何他們想要的地方打開程序,並且gif仍然在程序中。

這是我粘貼到我的cmd中創建我的單曲

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

還有另一個 - 我可以做的命令包括gif嗎? 例:

--includeFile=MYgif.gif

提前致謝

我強烈建議使用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

您需要編輯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)

我從這個鏈接中抓取了該函數使用PyInstaller捆綁數據文件(--onefile)

現在使用您編輯的spec文件構建程序。 'pyinstaller myProgram.spec'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM