当前位置:网站首页>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
边栏推荐
猜你喜欢

Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)

Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network

XPT2046 四线电阻式触摸屏

蚂蚁集团大规模图计算系统TuGraph通过国家级评测
![[network security] network asset collection](/img/3e/6665b5af0dedfcbc7bd548cc486878.png)
[network security] network asset collection

Loss function and positive and negative sample allocation: Yolo series

《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究

Leetcode skimming - remove duplicate letters 316 medium

Ant group's large-scale map computing system tugraph passed the national evaluation

6.12 critical moment of Unified Process Platform
随机推荐
6096. Success logarithm of spells and potions
Folium, diagnosis and close contact trajectory above
Strings and arrays
Party History Documentary theme public welfare digital cultural and creative products officially launched
【LeetCode】344-反转字符串
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
XPT2046 四线电阻式触摸屏
[leetcode] 1162 map analysis
使用 percona 工具给 MySQL 表加字段中断后该如何操作
密码学基础知识
C # get PLC information (kepserver) II
/bin/ld: 找不到 -lpam
6090. Minimax games
Leetcode skimming -- sum of two integers 371 medium
Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals
Name of institution approved in advance
[leetcode] 977 - carré du tableau ordonné
fastjson List转JSONArray以及JSONArray转List「建议收藏」
PHP static members
6092. 替换数组中的元素