当前位置:网站首页>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.pypack 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 --noconsoleNext, 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.specThis 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.pyWill 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.specThere'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
边栏推荐
- 6096. Success logarithm of spells and potions
- Review materials for the special topic of analog electronics with all essence: basic amplification circuit knowledge points
- 全是精华的模电专题复习资料:基本放大电路知识点
- PHP static members
- locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db‘: 没有那个文件或目录
- 高考分数线爬取
- [leetcode] 167 - sum of two numbers II - enter an ordered array
- Two traversal sequences are known to construct binary trees
- Thoroughly understand browser strong cache and negotiation cache
- Moveit obstacle avoidance path planning demo
猜你喜欢

XPT2046 四线电阻式触摸屏

Aike AI frontier promotion (7.2)

Bing. Site Internet

(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice

Aiko ai Frontier promotion (7.2)

【LeetCode】1254-统计封闭岛屿的数量

《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究

【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData

PTA ladder game exercise set l2-001 inter city emergency rescue

蚂蚁集团大规模图计算系统TuGraph通过国家级评测
随机推荐
数组和链表的区别浅析
floyed「建议收藏」
Custom exception
奥比中光 astra: Could not open “2bc5/[email protected]/6“: Failed to set USB interface
将点云坐标转换成世界坐标的demo
folium,确诊和密接轨迹上图
PTA ladder game exercise set l2-001 inter city emergency rescue
College entrance examination score line climbing
Golang MD5 encryption and MD5 salt value encryption
Leetcode skimming - remove duplicate letters 316 medium
高考分数线爬取
/bin/ld: 找不到 -lcrypto
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
Soul torture, what is AQS???
For the problem that Folium map cannot be displayed, the temporary solution is as follows
Two traversal sequences are known to construct binary trees
【LeetCode】577-反转字符串中的单词 III
Leetcode skimming -- count the number of numbers with different numbers 357 medium
[leetcode] 417 - Pacific Atlantic current problem
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network