[{"id":4658,"name":"4.OpenCV image quality","moduletypeid":782,"content":"<p><br\/><\/p><p style=\"white-space: normal; text-align: center; line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><strong><span style=\"font-size: 20px; font-family: Arial;\">OpenCV image quality<\/span><\/strong><\/span><\/p><p style=\"white-space: normal; line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><em><span style=\"font-size: 16px; font-family: Arial;\">Code path:<\/span><\/em><\/span><\/p><p style=\"white-space: normal; line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><em><span style=\"font-size: 16px; font-family: Arial; color: rgb(0, 112, 192);\"><\/span><\/em><\/span><\/p><p style=\"white-space: normal; line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><em><span style=\"font-size: 16px; font-family: Arial; color: rgb(0, 112, 192);\">\/home\/pi\/Yahboom_Project\/Raspbot\/1.OpenCV_course\/01Getting_started\/OpenCV\/03_OpenCV_image_quality.ipynb<\/span><\/em><\/span><\/p><p style=\"white-space: normal; line-height: 1.75em;\"><span style=\"font-family: Arial; font-size: 16px;\">1)&nbsp;Compression method<\/span><\/p><p style=\"white-space: normal; background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"background: rgb(255, 255, 0); font-family: arial, helvetica, sans-serif; font-size: 16px;\">cv2.imwrite(&#39;yahboomTest.jpg&#39;, img, [cv2.IMWRITE_JPEG_QUALITY, 50])<\/span><\/p><p style=\"white-space: normal; background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><span style=\"font-size: 16px; font-family: Arial; background: rgb(255, 0, 255);\">cv2.CV_IMWRITE_JPEG_QUALITY<\/span><span style=\"font-size: 16px; font-family: Arial;\">:&nbsp;Set the picture(.jpeg or .jpg)&nbsp;quality, value&nbsp;range&nbsp;is 0--100 (the larger the value, the higher the quality).&nbsp;Default value is 95<\/span><\/span><\/p><p style=\"white-space: normal; background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><span style=\"font-size: 16px; font-family: Arial; background: rgb(255, 0, 255);\">cv2.CV_IMWRITE_WEBP_QUALITY<\/span><span style=\"font-size: 16px; font-family: Arial;\">:&nbsp;Set the picture(.web)&nbsp;quality, value&nbsp;range&nbsp;is 0--100 (the larger the value, the higher the quality).&nbsp;<\/span><\/span><\/p><p style=\"white-space: normal; background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><span style=\"font-size: 16px; font-family: Arial; background: rgb(255, 0, 255);\">cv2.CV_IMWRITE_PNG_COMPRESSION<\/span><span style=\"font-size: 16px; font-family: Arial;\">:&nbsp;Set the picture(.png)&nbsp;quality, value&nbsp;range&nbsp;is 0--100 (the larger the value, the higher Compression ratio).&nbsp;Default value is 3.<\/span><\/span><\/p><table width=\"568\"><tbody><tr class=\"firstRow\"><td width=\"568\" valign=\"top\" style=\"padding: 0px 7px; border-color: rgb(0, 0, 0); word-break: break-all;\"><p style=\"background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><\/span><\/p><pre class=\"brush:as3;toolbar:false\"><p style=\"line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">import&nbsp;cv2<br\/>img&nbsp;=&nbsp;cv2.imread(&#39;yahboom.jpg&#39;,1)<br\/>cv2.imwrite(&#39;yahboomTest.jpg&#39;,&nbsp;img,&nbsp;[cv2.IMWRITE_JPEG_QUALITY,&nbsp;50])<br\/>#1M&nbsp;100k&nbsp;10k&nbsp;0-100&nbsp;Lossy&nbsp;compression<br\/><\/span><\/p><\/pre><\/td><\/tr><\/tbody><\/table><p style=\"white-space: normal; text-indent: 28px; background: rgb(255, 255, 255);\"><br\/><\/p><table width=\"568\"><tbody><tr class=\"firstRow\"><td width=\"568\" valign=\"top\" style=\"padding: 0px 7px; border-color: rgb(0, 0, 0); word-break: break-all;\"><p style=\"background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><\/span><\/p><pre class=\"brush:as3;toolbar:false\"><p style=\"line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">#&nbsp;1&nbsp;Lossy&nbsp;compression,&nbsp;2&nbsp;Transparency&nbsp;attribute<br\/>import&nbsp;cv2<br\/>img&nbsp;=&nbsp;cv2.imread(&#39;yahboom.jpg&#39;,1)<br\/>cv2.imwrite(&#39;yahboomTest.png&#39;,&nbsp;img,&nbsp;[cv2.IMWRITE_PNG_COMPRESSION,0])<br\/>#&nbsp;jpg&nbsp;0&nbsp;High&nbsp;compression&nbsp;ratio&nbsp;0-100&nbsp;&nbsp;png&nbsp;0&nbsp;Low&nbsp;compression&nbsp;ratio&nbsp;0-9<br\/><\/span><\/p><\/pre><\/td><\/tr><\/tbody><\/table><p style=\"white-space: normal; text-indent: 28px; background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">&nbsp;<\/span><\/p><table width=\"568\"><tbody><tr class=\"firstRow\"><td width=\"568\" valign=\"top\" style=\"padding: 0px 7px; border-color: rgb(0, 0, 0);\"><p style=\"background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><\/span><\/p><pre class=\"brush:as3;toolbar:false\"><p style=\"line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">#bgr8&nbsp;to&nbsp;jpeg&nbsp;format<br\/>import&nbsp;enum<br\/>import&nbsp;cv2<br\/>&nbsp;<br\/>def&nbsp;bgr8_to_jpeg(value,&nbsp;quality=75):<br\/>return&nbsp;bytes(cv2.imencode(&#39;.jpg&#39;,&nbsp;value)[1])<br\/><\/span><\/p><\/pre><\/td><\/tr><\/tbody><\/table><p style=\"white-space: normal; text-indent: 28px; background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">&nbsp;<\/span><\/p><table width=\"568\"><tbody><tr class=\"firstRow\"><td width=\"568\" valign=\"top\" style=\"padding: 0px 7px; border-color: rgb(0, 0, 0); word-break: break-all;\"><p style=\"background: rgb(255, 255, 255); line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\"><\/span><\/p><pre class=\"brush:as3;toolbar:false\"><p style=\"line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">import&nbsp;ipywidgets.widgets&nbsp;as&nbsp;widgets<br\/>&nbsp;<br\/>image_widget1&nbsp;=&nbsp;widgets.Image(format=&#39;jpg&#39;,&nbsp;)<br\/>image_widget2&nbsp;=&nbsp;widgets.Image(format=&#39;jpg&#39;,&nbsp;)<br\/>#&nbsp;create&nbsp;a&nbsp;horizontal&nbsp;box&nbsp;container&nbsp;to&nbsp;place&nbsp;the&nbsp;image&nbsp;widget&nbsp;next&nbsp;to&nbsp;eachother<br\/>image_container&nbsp;=&nbsp;widgets.HBox([image_widget1,&nbsp;image_widget2])<br\/>&nbsp;<br\/>#&nbsp;display&nbsp;the&nbsp;container&nbsp;in&nbsp;this&nbsp;cell&#39;s&nbsp;output<br\/>display(image_container)<br\/>&nbsp;<br\/>img1&nbsp;=&nbsp;cv2.imread(&#39;yahboomTest.jpg&#39;,1)<br\/>img2&nbsp;=&nbsp;cv2.imread(&#39;yahboomTest.png&#39;,1)<br\/>image_widget1.value&nbsp;=&nbsp;bgr8_to_jpeg(img1)<br\/>image_widget2.value&nbsp;=&nbsp;bgr8_to_jpeg(img2)<br\/><\/span><\/p><\/pre><\/td><\/tr><\/tbody><\/table><p style=\"white-space: normal; text-align: center; line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">&nbsp;<span style=\"font-size: 16px; font-family: Arial;\"><img src=\"http:\/\/www.yahboom.net\/public\/ueditor\/php\/upload\/image\/20200516\/1589629238997304.png\" title=\"1589629238997304.png\" alt=\"image.png\"\/><\/span><\/span><\/p><p style=\"white-space: normal; line-height: 1.75em;\"><span style=\"font-family: arial, helvetica, sans-serif; font-size: 16px;\">&nbsp;<\/span><\/p><p><br\/><\/p>","videotype":2,"videoname_1":"``","videoname_2":"``","videoname_3":"``","videoname_4":"``","videoname_5":"``","videoname_6":"``","newimg":0,"dataurl":"","customurl":"","documentation_switch":"","classificationid":304,"paixu":0,"adddate":"1612399221"},[]]