中文代码路径:
x
/home/jetson/Dofbot/4.opencv_CN/1.OpenCV入门/ 03_OpenCV图像质量.ipynb
英文代码路径
x/home/jetson/Dofbot/4.opencv_EN/1.OpenCV_Getting_started/03_OpenCV_image_quality.ipynb
1、压缩方式。
cv2.imwrite('yahboomTest.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 50])
cv2.CV_IMWRITE_JPEG_QUALITY: 设置图片格式为.jpeg或者.jpg的图片质量,其值为0---100(数值越大质量越高),默认95
cv2.CV_IMWRITE_WEBP_QUALITY: 设置图片的格式为.webp格式的图片质量,值为0--100
cv2.CV_IMWRITE_PNG_COMPRESSION: 设置.png格式的压缩比,其值为0--9(数值越大,压缩比越大),默认为3
主要代码如下:
import cv2
img = cv2.imread('yahboom.jpg',1)
cv2.imwrite('yahboomTest.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 50])
#1M 100k 10k 0-100 有损压缩
xxxxxxxxxx
# 1 无损 2 透明度属性
import cv2
img = cv2.imread('yahboom.jpg',1)
cv2.imwrite('yahboomTest.png', img, [cv2.IMWRITE_PNG_COMPRESSION,0])
# jpg 0 压缩比高0-100 png 0 压缩比低0-9
xxxxxxxxxx
#bgr8转jpeg格式
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
xxxxxxxxxx
import ipywidgets.widgets as widgets
image_widget1 = widgets.Image(format='jpg', )
image_widget2 = widgets.Image(format='jpg', )
# create a horizontal box container to place the image widget next to eachother
image_container = widgets.HBox([image_widget1, image_widget2])
# display the container in this cell's output
display(image_container)
img1 = cv2.imread('yahboomTest.jpg',1)
img2 = cv2.imread('yahboomTest.png',1)
image_widget1.value = bgr8_to_jpeg(img1)
image_widget2.value = bgr8_to_jpeg(img2)
代码块运行到最后,可以看到两种照片的对比图。