所以我得到main.py
我通过PyInstaller main.exe
变成了--onefile
,但main.exe
仍然需要img.png
文件夹中的图像文件\data
它与main.py/main.exe
...
img_l = pygame.image.load(os.path.join('data', 'img.png'))
screen.blit(img_l, (0, 0))
如何正确包装这些图像?我现在用pygame.image.tostring
搞乱了一段时间,然后尝试将其保存到.txt
文件中,以便我可以使用.txt
文件夹中的data
文件代替img.png
的{{1}},然后我可以使用pygame.image.fromstring
(也许),但我还没弄明白如何让它发挥作用。
我甚至不确定这是否正确/可行。
任何想法/建议真诚地赞赏。
答案 0 :(得分:1)
我对fromstring
和tostring
方法了解不多,但您可以随时提供
图像为base64数据。 Pygame似乎需要一个实际的图像文件而不仅仅是一个blob
二进制数据所以在下面的例子中,我已经包含3个小图标作为base64字符串
写入名为“data”的子文件夹中的文件。然后传递文件名
到pygame.image.load()
。
我用pyinstaller --onefile filename.py
测试了这段代码
它没有任何手动更改设置/规格/路径等工作正常。
import os
import hashlib
import pygame
import time
import base64
def create_assets(asset_dict, asset_dir):
"""
hand this function a dictionary of assets (images, mp3s, whatever)
and an absolute path to the data/asset folder.
The function creates the folder and files from the base64 strings
if they don't exist. If the files exist, an md5 check is run
instead to ensure integrity
"""
first_run = False
if not os.path.isdir(asset_dir):
os.mkdir(asset_dir)
first_run = True
for label in asset_dict:
asset = asset_dict[label]
filename = os.path.join(asset_dir, asset["filename"])
rewrite = False
# no need to check file if we just created the data folder
if not first_run:
if not os.path.isfile(filename):
# the file doesn't exist
rewrite = True
else:
# file exists - make sure it's intact via md5
with open(filename, "rb") as f:
if not hashlib.md5(f.read()).hexdigest() == asset["md5"]:
# the filename exists but the contents is wrong
rewrite = True
if first_run or rewrite:
# one of our checks failed or first run - write file
print ("Writing file: ",filename)
with open(filename, "wb") as f:
f.write(base64.b64decode(asset["data"]))
else:
print ("File exists: ",filename)
"""
This the data dictionary. It's very easy to save
the whole thing as json should you feel like it.
The images are just small, random icons at the moment
"""
assets = {
"background": {
"filename": "bg1.png",
"data": "iVBORw0KGg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KobvwUkaB9wApAIx7H1O5h5+Jv5pFYAjT05r3maygHfIR8OxZlTJs+uERoKI3iEf1kN2HKdc2mJoJEjme1ILK/WyP1IcvNFTFpZfy7H0coaUP4qgKIqSC6exeOrJr2YxuSxM3x1nM5Hn7+8MpTK31Wme/WgTnsud2Puc2npyMlIMVI3p++O0XDiM44SLVCDGt2d+8itZdpOt10n77X7MrXUAGEtnkPJH2c5uItlNLI8t7hlW19FA68V2alusmickv0aVSgD7lR4oUQ8AgGAU8Vz1IdaIVT09UIabg9eHo+9DbMkbZYGeK514h3wIkkjKH9X1CpEcciBGfjVL5F0IeXa9CEUPgFgj4rnWRXg0yE5+u6pXKUEOxpXwaJDExNp/za+ahI/n3ip7f7a36rscRShVd9oHFFWmZosGxWjrdSJPxYoLLguFPzkA2i51FHesNfDr1UxVD6AQybHw9AtmtxXD8/HXw/Y+J/LsOu23+tlKb5BfyZKZjyNIIuHRIMpWcSp6nirfveNkFpIY0/MJlscWkRpMxXtklUhMrLGzsVN2CkDXU+en/lv2ltUKUu9dNakAwm8WqKw+7S2rFZSei+8apgKwdTciGEV+vpgqqz7tLatKTka0naEclBbqsmB2W+l5NEhl9RnOLw0MpwIxbN0OJFsNtW4rB8560QOlSgXQdMaDZDcBEHw8gWSrwahXQdVAVQIonb1aff8A8A1zY9iTMCcAAAAASUVORK5CYII=",
"md5": "79f25f0784a7849415f9c3d0d9d05267"
},
"weapon": {
"filename": "sword1.png",
"data": "iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAACzUlEQVQ4jZWVX0hTYRjGfydHZJb5J03SrIarrQaNEhKERgT25yaiIjEGQUWB0U03BUHWRVS3BUUOHGkUFg2LjW5bQkUjs6bNHcskJZRVplHTzs7bxdphy0Ot5+Ycnu99n3O+5/l4P+Q32u7FxO0JyOWOPvkbWjujouu68e72BOTqrX5j3UIG9mxdQXOTA4BI7AvB0Aca6itxOUoNruPBW5y2Yupc5RzauxoALakbGkrbvZgA1FQXUr++HEVR8PlVfH7VKGrcYeVoo51L3lcEQyNsql3CueMbjPWnL8eJDn0FIG9i7raWmuWF7G5YgaIoALgcpThtRWiacGSfnZ1bqlEUhYJ8Cw+7Rzm8dzXVSxcYglUVBYQjcW50DcLljj7Dk1zwt/rWzqgoIiL8gSs3+4m+myCiTmC3LqJ27WISM8lZXNpD750BDu5ZldqhiMjrgc9ysbVXevrjIiJy5Ey36Louw6NTouu6nLjwzJRL97o9AXnSM5ZKOTOAYGiExh1Wou++MjQyhXVZIeOffhDuiwOYcsHQB+NZ5yrHcmCXDaetiOCjERrqK9m4roxwJM7KqoWMf/pBeWk+LnsJ375rsziAhvpKgqFUL4Cph9duv2FweIpwXxyXvQSXo5TEtDaLO7DLZnje3ORAURTmpEW8dwZIa2tJITGjAZCYSaJpuimXxrH9a4wjh5mx/xtKZqAWM2NzDcUsUIuZsTXVhTmFYhaoEUqmsf8bShZaO6M5japc0N6lSt7kvO0tmqazfu3irA9FYl+44H2Fw1pEVUWBwfn8KgX5FirK5mfVd9wfxHs3Rt7JU6dbdBE0TYxGAJ9fRR2eZOZnks0bl+Lzq5y/3os6PMnD7lES00lqnamfCEfivB+dwmkrTh0bkdTEztx+T39c3J6APA5/NLjnr8fl7JUX8qRnzLCivUvNmthZgm5PIEv0X6OtvUuddQX8Ag5COzDf7kUwAAAAAElFTkSuQmCC",
"md5": "92485d36b8ac414cc758d9a6c6f28d23"
},
}
# get absolute path to asset directory
asset_dir = "data"
asset_dir_path = os.path.join(os.getcwd(), asset_dir)
# create files in asset directory using the assets dictionary
create_assets(assets, asset_dir_path)
pygame.init()
WIDTH = 800
HEIGHT = 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
loaded_images = {}
# initalize/load all the newly created images
for label in assets:
file_path = os.path.join(asset_dir_path,assets[label]["filename"])
loaded_images[label] = pygame.image.load(file_path)
pos1 = 0
pos2 = 0
t_start = time.time()
while time.time() - t_start < 5:
for img in loaded_images:
SCREEN.blit(loaded_images[img], (pos1, pos2))
time.sleep(0.2)
pos1 += 20
pos2 += 20
pygame.display.update()
我将图像转换为base64字符串,如下所示:
import base64
with open(img_input, "rb") as f:
with open(img_output_b64, "wb") as f2:
f2.write(base64.b64encode(f.read()))