效果图:
(1)压缩前:10.9M
(2) 压缩后:1.46M
(3) 直接上代码 ,
# 设置压缩质量 0-100 ,0最差,100最好compression_params = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
import cv2 import osPATH = 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+=1resizeImage(childPATH,NoResize)print("总数量:" + str(total))print('------修改图片大小全部完成❥(^_-)')