效果图:
(1)压缩前:10.9M
(2) 压缩后:1.46M
(3) 直接上代码 ,
# 设置压缩质量 0-100 ,0最差,100最好 compression_params = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
import cv2 import os PATH = r'F:\aa_jpg' # 压缩该路径下的图片,压缩后会保存到原路径 def resizeImage(file,NoResize): # 读取图片 image = cv2.imread(file) # 设置压缩质量 0-100 ,0最差,100最好 compression_params = [int(cv2.IMWRITE_JPEG_QUALITY), 50] if image is None: NoResize += [str(file)] print("***没压缩:" + file) else: # 写入压缩后的图片,这里的'output.jpg'是压缩后的文件名 cv2.imwrite(file, image, compression_params) print("压缩:"+file) if __name__ == "__main__": if True: # 批量压缩图片分辨率 NoResize = [] # 记录没被修改的图片 total = 0 # 子文件夹 for childPATH in os.listdir(PATH): # 子文件夹路径 childPATH = PATH + '\\' + str(childPATH) print(childPATH) total+=1 resizeImage(childPATH,NoResize) print("总数量:" + str(total)) print('------修改图片大小全部完成❥(^_-)')
标签:__,childPATH,Python,压缩,cv2,jpg,file,print From: https://www.cnblogs.com/technicist/p/18338068