Welcome to DOFBOT-Ultra repository
4. Image Quality
Image Quality
Code path:
~/dofbot_opencv/scripts/1.OpenCV_basic/03_quality_pic.ipynb
- Compression method.
cv2.imwrite('yahboomTest.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 50])
cv2.CV_IMWRITE_JPEG_QUALITY: Sets the image quality for .jpeg or .jpg format, with a value range of 0---100 (larger value means higher quality), default is 95
cv2.CV_IMWRITE_WEBP_QUALITY: Sets the image quality for .webp format, with a value range of 0--100
cv2.CV_IMWRITE_PNG_COMPRESSION: Sets the compression ratio for .png format, with a value range of 0--9 (larger value means higher compression), default is 3
Main code:
xxxxxxxxxximport cv2img = cv2.imread('yahboom.jpg',1)cv2.imwrite('yahboomTest.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 50])#1M 100k 10k 0-100 lossy compressionxxxxxxxxxx# 1 lossless 2 transparency attributeimport cv2img = cv2.imread('yahboom.jpg',1)cv2.imwrite('yahboomTest.png', img, [cv2.IMWRITE_PNG_COMPRESSION,0])# jpg 0 high compression 0-100 png 0 low compression 0-9xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])xxxxxxxxxximport ipywidgets.widgets as widgetsimage_widget1 = widgets.Image(format='jpg', )image_widget2 = widgets.Image(format='jpg', )# create a horizontal box container to place the image widget next to eachotherimage_container = widgets.HBox([image_widget1, image_widget2])# display the container in this cell's outputdisplay(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)After the code block runs, you can see a comparison of the two images.
