import cv2 # 读取图片 img = cv2.imread("/opt/data/header.png") # 设置压缩质量(0-100,值越小压缩越狠,质量越差) quality = 50 # 写入压缩后的图像(注意必须是 .webp) cv2.imwrite( "/opt/data/output_small.webp", img, [int(cv2.IMWRITE_WEBP_QUALITY), quality], ) # # 读取原图 # img = cv2.imread("/opt/data/header.png") # # # 缩放图像(例如缩小为原图的一半) # resized = cv2.resize(img, (img.shape[1] // 2, img.shape[0] // 2)) # # # 写入压缩图像,降低质量 # cv2.imwrite("/opt/data/output_small.webp", resized, [int(cv2.IMWRITE_WEBP_QUALITY), 40])