当前位置:网站首页>Pyinstaller's method of packaging pictures attached to exe
Pyinstaller's method of packaging pictures attached to exe
2022-07-02 15:46:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
2019.10.27 to update
Recent wordpress The project learned that the picture bytecode can exist in the file , So I did the experiment and succeeded ( It's a bunch of bytecodes in the code ……)
Reference resources :
https://blog.csdn.net/jss19940414/article/details/85841982
https://www.cnblogs.com/xianqingsong/p/9965899.html
https://blog.csdn.net/u013055678/article/details/71406746
This is an example of encoding, decoding and saving pictures under the path :
import os, base64
with open("D:\\XiutuDog\\_pic.png","rb") as f:
# b64encode Is the code ,b64decode It's decoding
base64_data = base64.b64encode(f.read())
print(base64_data)# Output generated base64 code
#img_str = 'abcdefgh12345oK='# For example, the generated code is put like this , Replace the following base64_data that will do
img_data = base64.b64decode(base64_data)
# Be careful : If it is "data:image/jpg:base64,", Then what you keep should be png Format , If it is "data:image/png:base64," Then when you save it, use jpg Format .
with open('001.png', 'wb') as f:
f.write(img_data)
Put the encoded base64_data( Output ) Put it in the code , Then decode and write to the folder “ Release ”【 In the annotated img_str Where? 】, Although the method is stupid , But this is true “exe Attached picture ”!
2018.10.13 additional
It turns out that the following method is to save the image to the absolute path ( The same folder is a relative path ……), So it can be used anywhere …… There's no use for eggs now , I'll find another way ……
pyinstaller Package run error failed to execute script main( It is also possible that the image path is not found ):https://blog.csdn.net/jeff_/article/details/72907113
original text :
The tool written by BiShe just remembered recently that it should be made into exe Benefit the public , However, the road twists and turns
First, vs Direct mount pyinstaller No use ; After that, I don't know how to type multiple files ( Finally, write it in a .py In the …… also import Try to reduce the number of bags ); Finally, image files are not packaged , Let me have a seed I feel uncomfortable when I can't clean my excrement —— One small exe It must also be in a folder with several fixed named pictures, otherwise it cannot be opened !!
I read many posts today , Some methods are simple but unsuccessful ; Some trouble , There are a lot of things to change, so I don't want to try ; Finally, looking at the error prompt and thinking, I finally successfully hit exe!
pyinstaller Of install :
reference ( Good post )https://blog.csdn.net/lqzdreamer/article/details/77917493
Single exe If you don't use anything , The following commands are enough .
pyinstaller -F test.py
pack python Add icon Get rid of cmd Black window https://blog.csdn.net/frank_good/article/details/60962012
python pyinstaller.py -F -p C:\python27; -i .\xxx.ico .\demo.py --noconsole
Next, I will introduce the method of packaging pictures .
The beginning is reference https://blog.csdn.net/qq744746842/article/details/49404027 Of , But then the attempt failed ,
pyinstaller options name.spec
This code can't be executed ……
The whole process of this tutorial begins :
Generate spec file :
This is what I want to pack .( The following file names refer to this )
then cmd To this directory ,( I press and hold the current folder directly shift+ Right click , single click “ Open here powershell window ”, The same effect is also convenient )
stay cmd Internal input :
pyi-makespec FP.py
Will generate a FP.spec
Then change FP.spec What's inside , Here is the original
Directly type the package of this file, and the result is …… as follows , I don't want to make the tools so complicated ……
The next step is to change the contents ,( If you don't want to use this method , The other is https://blog.csdn.net/jirryzhang/article/details/78881512 What is in is to do with instructions , And directly a single exe)
.py Picture reference full path
Before the reform , You need to python Replace all image references in the program with complete paths :
Just use pictures in the code ……
This method has reference http://tieba.baidu.com/p/3060401749 Of the 6 floor ( Look at him jia Added function , Just get the full path ), Add another post to see “pyinstaller Not compiling code , Instead, make the script into an executable file , So don't expect the program to be faster after packaging ”, Guess from this “ The picture needs a full path ,spec The full path should also be written in the file , So we can make this exe‘ Connect ’ Run up ”
Start to change spec file :
The method has reference https://blog.csdn.net/xinyingzai/article/details/80282856,
however datas Not at all datas+=[(‘_pic.png’,’D:\\XiutuDog\\_pic.png’,’DATA’)], Will report a mistake “expected tuple,not list”,
So I changed it like this datas+=((‘_pic.png’,’D:\\XiutuDog\\_pic.png’,’DATA’))
The red box is three pictures I need , The format is ((‘ name 1′,’1 Full path to ’,’ type ’),(‘ name 2′,’2 Full path to ’,’ type ’))
The orange arrow on the left shows whether the console is displayed cmd Black box
The orange arrow on the right is added , You can give it to yourself exe Change the icon 【 But leaving the folder is not this icon , It's not solved yet 】
The full text is as follows :
# -*- mode: python -*-
block_cipher = None
a = Analysis(['FP.py'],
pathex=['D:\\XiutuDog'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
a.datas +=(('_icon.ico','D:\\XiutuDog\\_icon.ico','DATA'),
('_pic.png','D:\\XiutuDog\\_pic.png','DATA'),
('_start.png','D:\\XiutuDog\\_start.png','DATA'))
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[('_icon.ico','D:\\XiutuDog\\_icon.ico','DATA')],
name='FP',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False , icon='_icon.ico')
Start packing
Different from usual , The object of this time It was changed before .spec file , instead of .py file
pyinstaller -F FP.spec
There's a hole : If it has just been changed .spec, A cheap hand hit pyinstaller -F test.py , that .spec The file can be changed to the default !
After completion, it should be like this (FP.exe It's from dist Folder copied ,【dist It's packed in the folder exe】)
that exe, Leaving the folder where the icon is located may change back to the original default icon , It will recover in a while ……
But anyway , You can run away from the picture o(* ̄▽ ̄*)ブ
Some other references
https://blog.csdn.net/xueyuanlzh/article/details/79892968
https://blog.csdn.net/lion_cui/article/details/51329497( Picture turn py, Feel in trouble , It doesn't work )
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/147721.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢
动态规划入门一,队列的bfs(70.121.279.200)
【LeetCode】417-太平洋大西洋水流问题
Leetcode skimming - remove duplicate letters 316 medium
Deux séquences ergodiques connues pour construire des arbres binaires
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
已知两种遍历序列构造二叉树
Ant group's large-scale map computing system tugraph passed the national evaluation
彻底弄懂浏览器强缓存和协商缓存
PHP static members
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
随机推荐
全是精华的模电专题复习资料:基本放大电路知识点
PHP static members
Redux——详解
/bin/ld: 找不到 -lpam
/bin/ld: 找不到 -llz4
Demo of converting point cloud coordinates to world coordinates
爱可可AI前沿推介(7.2)
folium,确诊和密接轨迹上图
【LeetCode】877-石子游戏
6096. 咒语和药水的成功对数
Why does the system convert the temp environment variable to a short file name?
matlab中wavedec2,说说wavedec2函数[通俗易懂]
PostgresSQL 流复制 主备切换 主库无读写宕机场景
2279. Maximum number of backpacks filled with stones
奥比中光 astra: Could not open “2bc5/[email protected]/6“: Failed to set USB interface
【LeetCode】417-太平洋大西洋水流问题
ssh/scp 使不提示 All activities are monitored and reported.
[leetcode] 1254 - count the number of closed Islands
6095. 强密码检验器 II
[leetcode] 977 - carré du tableau ordonné