今天想弄一下这个,记录一下过程:
1.
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
pyinstaller vei.py
执行这个之后生成了build目录和exe文件,但发现exe存在闪退问题:
查了一下说缺少相关包,继续下载:
pip install pypiwin32 -i https://pypi.tuna.tsinghua.edu.cn/simple
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycairo下载相关包复制到当前项目下:
pip install pycairo-1.20.0-cp39-cp39-win_amd64.whl
pip install PyQt5
pip install IPython
pip install wxPython
pip install ipykernel
pip install zmq
一些七七八八的包,也不知道干什么用的,查到有文章说要下载这些包。
然后在命令行输入pyinstaller 说不是可执行程序,于是在命令行全局安装 pip intall pyinstaller
用参数-F 这样dist下面只有exe文件
报错信息显示PIL模块找不到,于是:安装PIL显示失败,于是安装pillow
C:\Windows\system32>pip install PIL
ERROR: Could not find a version that satisfies the requirement PIL
ERROR: No matching distribution found for PIL
C:\Windows\system32>pip install Pillow
Collecting Pillow
Using cached Pillow-8.1.1-cp39-cp39-win_amd64.whl (2.2 MB)
Installing collected packages: Pillow
Successfully installed Pillow-8.1.1
不知道执行这个exe还是显示模块找不到,于是重新写了一个py文件再次打包:
已经可以执行了。说明运行exe成功。
另外运行一段修改png为jpg的拒绝访问问题:
import os
from PIL import Image
import cv2 as cv
def PNG_JPG(PngPath):
img = cv.imread(PngPath, 0)
w, h = img.shape[::-1]
infile = PngPath
outfile = os.path.splitext(infile)[0] + ".jpg"
img = Image.open(infile)
img = img.resize((int(w / 2), int(h / 2)), Image.ANTIALIAS)
try:
if len(img.split()) == 4:
# prevent IOError: cannot write mode RGBA as BMP
r, g, b, a = img.split()
img = Image.merge("RGB", (r, g, b))
img.convert('RGB').save(outfile, quality=70)
os.remove(PngPath)
else:
img.convert('RGB').save(outfile, quality=70)
os.remove(PngPath)
return outfile
except Exception as e:
print("PNG转换JPG 错误", e)
path_root = os.getcwd()
Path= 'C:\\Users\\pic\\'
img_dir = os.listdir(Path)
for img in img_dir:
if img.endswith('.png'):
PngPath= Path + img
PNG_JPG(PngPath)
img_dir = os.listdir(Path)
for img in img_dir:
print(img)
一开始一直报错显示permission denied
方法:用管理员身份运行pycharm就好了
再次打开文件,后缀已经修改。