如何在python中使用opencv压缩png文件? [英] How to compress png file with opencv in python?

查看:1840
本文介绍了如何在python中使用opencv压缩png文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这段代码:

compression_params = [cv2.CV_IMWRITE_PNG_COMPRESSION, 9] 
img = cv2.imread('img1.png', cv2.IMREAD_UNCHANGED) 
cv2.imwrite('compress_img1.png', img, compression_params)

但是我得到了这个错误:

But I obtain this error:


AttributeError:module'cv2'没有属性'CV_IMWRITE_PNG_COMPRESSION'

AttributeError: module 'cv2' has no attribute 'CV_IMWRITE_PNG_COMPRESSION'

我正在使用python 3.5和opencv 3.0

I'm working with python 3.5 and opencv 3.0

推荐答案

OpenCV 3.0中的名称是 IMWRITE_PNG_COMPRESSION (不含 CV _ 前缀)。

The name in OpenCV 3.0 is IMWRITE_PNG_COMPRESSION (without the CV_ prefix).

所以试试:

cv2.imwrite('compress_img1.png', img,  [cv2.IMWRITE_PNG_COMPRESSION, 9])






这篇文章还提到要转换为 int 。我不确定是否还需要这个:


This post mentions also to cast to int. I'm not sure if this is still needed:

cv2.imwrite('compress_img1.png', img,  [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

这篇关于如何在python中使用opencv压缩png文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆