image - 如何在python中用opencv压缩png文件?

标签 image python-3.x numpy opencv3.0

我试过这个代码:

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/

相关文章:

html - 如何使 "image banner"100% 的视口(viewport)没有边距,以及页面中心的其余部分

python - 诗歌 + Cython + 测试(Nosetests)

python - 你能用turtle.write获取用户输入吗?

python - 如何在 VSCode 中运行 python3 代码?/bin/sh : 1: python: not found

python - 如何索引pandas Dataframe KeyError : False中的数字值

python - 为什么使用 read_csv() 创建的 DataFrame 与使用相同数据的 DataFrame() 创建的另一个 DataFrame 不同?

python - 删除 pandas 中的 nan 后出现 keyerror

python - 在给定 XY 边界之间的 Matplotlib 图中显示图像

python - 如何将字节保存为图像?

html - 图像映射区域聚合某些坐标 Firefox 不工作,IE 工作