{"id":9772,"date":"2020-08-09T23:04:55","date_gmt":"2020-08-09T17:34:55","guid":{"rendered":"https:\/\/java2blog.com\/?p=9772"},"modified":"2020-12-23T22:40:31","modified_gmt":"2020-12-23T17:10:31","slug":"cv2-imwrite-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/cv2-imwrite-python\/","title":{"rendered":"Python | cv2 imwrite() Method"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Syntax\">Syntax<\/a><ul><li><a href=\"#Parameters\">Parameters<\/a><\/li><li><a href=\"#Return_Value\">Return Value<\/a><\/li><\/ul><\/li><li><a href=\"#cv2_imwrite_method_Examples\">cv2 imwrite() method Examples<\/a><\/li><\/ul><\/div>\n<p>In this tutorial, we will see how to save an image in your own system using python by using open-cv which exists as cv2 (computer vision) library.<\/p>\n<p>You can use <code>imwrite()<\/code> method of <code>cv2<\/code> library to save an image on your system. To use cv2 library, you need to import cv2 library using <code>import statement<\/code>.<\/p>\n<p>Now let&#8217;s see the syntax and return value of <code>imwrite()<\/code> method, then we will move on to the examples.<\/p>\n<h2><span id=\"Syntax\">Syntax<\/span><\/h2>\n<pre class=\"lang:python decode:1 \" >\ncv2.imwrite(path, image)\n<\/pre>\n<h3><span id=\"Parameters\">Parameters<\/span><\/h3>\n<p>You need to pass two parameters to imwrite() method. Parameters are:<\/p>\n<ol>\n<li><code>path:<\/code> Location address where you want to save an image in your system in string form with including the filename.\n<p>here two cases will be possible\u00a0:<\/p>\n<p><code>i)<\/code> if you want to save an image in the current working directory then we have to mention only the name of the image with their extension like <code>.jpg<\/code>,<code>.png<\/code> etc.<\/p>\n<p><code>ii)<\/code> if you want to save an image at somewhere else in your system not in the current working directory then we have to give complete path, also known as, absolute path of the image.\n<\/li>\n<li> <code>image:<\/code> It is the image pixel matrix of that image, which you want to save in your system.<\/li>\n<\/ol>\n<h3><span id=\"Return_Value\">Return Value<\/span><\/h3>\n<p>It returns either True or False. Return <code>True<\/code> if the image is successfully saved otherwise return <code>False<\/code>. <\/p>\n<h2><span id=\"cv2_imwrite_method_Examples\">cv2 imwrite() method Examples<\/span><\/h2>\n<p>Now Let&#8217;s see the Python code\u00a0:<\/p>\n<pre class=\"lang:python decode:1 \" >\n# import computer vision library(cv2) in this Program\nimport cv2\n\n# import os library in this program\nimport os\n\n# main code\nif __name__ == \"__main__\"\u00a0:\n\n\u00a0 \u00a0 # mentioning absolute path of the image\n\u00a0 \u00a0 img_path = \"C:\\\\Users\\\\user\\\\Desktop\\\\flower.jpg\"\n\n\u00a0 \u00a0 # read an image\n\u00a0 \u00a0 image = cv2.imread(img_path)\n\n\u00a0 \u00a0 print(\"Before saving an image\u00a0,contents of directory shows\u00a0:\")\n\n\u00a0 \u00a0 # shows the contents of given directory\n\u00a0 \u00a0 print(os.listdir(\"C:\\\\Users\\\\user\\\\Desktop\\\\save image\"))\n\u00a0 \u00a0 \n\u00a0 \u00a0 # mentioning absolute path \n\u00a0 \u00a0 save_img_path = \"C:\\\\Users\\\\user\\\\Desktop\\\\save image\\\\image1.jpg\"\n\u00a0 \u00a0 \n\u00a0 \u00a0 # save an image at the given\u00a0mention path\n\u00a0 \u00a0 cv2.imwrite(save_img_path,image)\n\n\u00a0 \u00a0 print(\"After saving an image\u00a0,contents of directory shows\u00a0:\")\n\n\u00a0 \u00a0 # shows the contents of given directory\n\u00a0 \u00a0 print(os.listdir(\"C:\\\\Users\\\\user\\\\Desktop\\\\save image\"))\n<\/pre>\n<p>Output\u00a0:<\/p>\n<div class=\"content-box-green\">\nBefore saving an image, contents of directory shows\u00a0:<br \/>\n[]\nAfter saving an image, contents of directory shows\u00a0:<br \/>\n[&#8216;image1.jpg&#8217;]\n<\/div>\n<p>That&#8217;s all about cv2.imwrite() Method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsSyntaxParametersReturn Valuecv2 imwrite() method Examples In this tutorial, we will see how to save an image in your own system using python by using open-cv which exists as cv2 (computer vision) library. You can use imwrite() method of cv2 library to save an image on your system. To use cv2 library, you need [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,197],"tags":[196,195],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9772"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=9772"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9772\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=9772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=9772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=9772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}