''' pip install numpy pip install opencv-python ''' from io import BytesIO import cv2 import numpy as np def pic_compress(pic_path, out_path, target_size=199, quality=90, step=5, pic_type='.jpg'): # 读取图片bytes with open(pic_path, 'rb') as f: pic_byte = f.read() img_np = np.frombuffer(pic_byte, np.uint8) img_cv = cv2.imdecode(img_np, cv2.IMREAD_ANYCOLOR) current_size = len(pic_byte) / 1024 print("图片压缩前的大小为(KB):", current_size) while current_size > target_size: pic_byte = cv2.imencode(pic_type, img_cv, [int(cv2.IMWRITE_JPEG_QUALITY), quality])[1] if quality - step < 0: break quality -= step current_size = len(pic_byte) / 1024 # 保存图片 with open(out_path, 'wb') as f: f.write(BytesIO(pic_byte).getvalue()) return len(pic_byte) / 1024 def main(): pic_size = pic_compress('img.png', '01new_test.jpg', target_size=100) print("图片压缩后的大小为(KB):", pic_size) if __name__ == '__main__': main()
标签:高保真,img,Python,pic,cv2,高压缩,np,byte,size From: https://www.cnblogs.com/zwnsyw/p/17649448.html