我试过这个代码:
compression_params = [cv2.CV_IMWRITE_PNG_COMPRESSION, 9]
img = cv2.imread('img1.png', cv2.IMREAD_UNCHANGED)
cv2.imwrite('compress_img1.png', img, compression_params)
但我得到这个错误:
AttributeError: module 'cv2' has no attribute 'CV_IMWRITE_PNG_COMPRESSION'
我正在使用 python 3.5 和 opencv 3.0
最佳答案
OpenCV 3.0 中的名称是 IMWRITE_PNG_COMPRESSION
(没有 CV_
前缀)。
所以尝试:
cv2.imwrite('compress_img1.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 9])
This post还提到投到
int
.我不确定这是否仍然需要:cv2.imwrite('compress_img1.png', img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
关于image - 如何在python中用opencv压缩png文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027678/