[Python] 纯文本查看 复制代码import os
import cv2
from multiprocessing import Pool
from functools import partial
def compress_image(input_path, target_size_kb=100):
img = cv2.imread(input_path)
# 调整图像质量以满足目标大小
quality = 75
while os.path.getsize(input_path) > target_size_kb * 1024:
success, encoded_img = cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), quality])
if success:
with open(input_path, 'wb') as f:
f.write(encoded_img)
quality -= 5
if quality
代码可以完美运行,但我用Pyinstaller打包后,运行exe程序后发现并没有顺利跑下来,而是循环几次填写图片路径后才可以将图片体积压缩小,压缩结束后想换下一批图片进行压缩 需要重新打开exe文件运行,不知有没有大佬可以找到问题并解决这个不完美的代码