当前位置:网站首页>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
边栏推荐
- Aike AI frontier promotion (7.2)
- Jsp+mysql006 community management system
- 已知两种遍历序列构造二叉树
- /bin/ld: 找不到 -lpam
- 数组和链表的区别浅析
- 高考分数线爬取
- Wechat Alipay account system and payment interface business process
- ssh/scp 使不提示 All activities are monitored and reported.
- (4) Flink's table API and SQL table schema
- 【idea】推荐一个idea翻译插件:Translation「建议收藏」
猜你喜欢
Leetcode skimming -- sum of two integers 371 medium
[salesforce] how to confirm your salesforce version?
全是精华的模电专题复习资料:基本放大电路知识点
Redux——详解
Bing. Site Internet
Review materials for the special topic of analog electronics with all essence: basic amplification circuit knowledge points
Deux séquences ergodiques connues pour construire des arbres binaires
PTA ladder game exercise set l2-001 inter city emergency rescue
Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
PHP static members
随机推荐
Folium, diagnosis and close contact trajectory above
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
奥比中光 astra: Could not open “2bc5/[email protected]/6“: Failed to set USB interface
SQL修改语句
MD5 encryption
[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
2278. 字母在字符串中的百分比
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db‘: 没有那个文件或目录
PyObject 转 char* (string)
(4) Flink's table API and SQL table schema
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
Moveit 避障路径规划 demo
使用 percona 工具给 MySQL 表加字段中断后该如何操作
Leetcode skimming -- incremental ternary subsequence 334 medium
【LeetCode】1905-统计子岛屿
【LeetCode】877-石子游戏
[leetcode] 1162 map analysis
SQL FOREIGN KEY
[leetcode] 417 - Pacific Atlantic current problem