

{"id":74471,"date":"2020-01-15T12:22:04","date_gmt":"2020-01-15T06:52:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=74471"},"modified":"2026-04-28T10:20:38","modified_gmt":"2026-04-28T04:50:38","slug":"read-display-save-image-opencv","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/","title":{"rendered":"How to Read, Display and Save Image in OpenCV &#8211; Step-by-step Guide"},"content":{"rendered":"<p>In the last tutorial, we successfully <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/install-opencv-on-ubuntu\/\">set up the OpenCV environment<\/a><\/strong><\/em>. Now we can finally start learning the basics. In this section, we\u2019ll learn more than just typing commands and start seeing actual results. We\u2019ll learn how to pick up an image file, show it on your screen, and even save a new version after making changes.<\/p>\n<p>To write code, you can use any editor of your choice.<\/p>\n<h3>Steps to Read, Display, and Save an Image in OpenCV<\/h3>\n<h4>Reading Images using OpenCV<\/h4>\n<p>To read the contents of an image, we have a function <strong><code>cv2.imread().<\/code><\/strong><\/p>\n<p><strong><code><\/code><\/strong>It loads the file as a NumPy array where each cell stores a pixel\u2019s color. The image should be in the same directory. If not, then the full path of the image should be given.<\/p>\n<p><strong>The function takes two arguments :<\/strong><\/p>\n<p>1. The first argument is the path of the image.<\/p>\n<p>2. The second argument is a flag that describes the way the image is read. The default is:<\/p>\n<ul>\n<li><strong>cv2.IMREAD_COLOR:<\/strong> Loads image in color. It is used by default.<\/li>\n<li><strong>cv2.IMREAD_GRAYSCALE:<\/strong> Loads image in gray mode.<\/li>\n<li><strong>cv2.IMREAD_UNCHANGED:<\/strong> Loads image as it is, including alpha channels.<\/li>\n<\/ul>\n<p><strong>Your first code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nimport cv2\r\n\r\n# loads the image in color\r\nimg = cv2.imread(\u201ccat.jpg\u201d)\r\n\r\n# loads the image in grayscale \r\ngray_img = cv2.imread(\u201cimage.jpg\u201d, cv2.IMREAD_GRAYSCALE)\r\n<\/pre>\n<p>As you can see, we also import numpy in our program. OpenCV uses numpy, and with numpy, we can easily manipulate the data. After running this program in Python, we will be assigning the image data to the img variable.<\/p>\n<p><em><strong>Work on the <a href=\"https:\/\/data-flair.training\/blogs\/project-in-python-colour-detection\/\">Color Detection Python Project<\/a> using OpenCV\u00a0<\/strong><\/em><\/p>\n<h4>Displaying Images using OpenCV<\/h4>\n<p>To display an image in the window, we have a function <strong><code>cv2.imshow()<\/code><\/strong>. This function creates a window and displays the image at its original size.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nimport cv2\r\n\r\n# load image in color\r\nimg = cv2.imread(\u201ccat.jpg\u201d)\r\n#load image in grey\r\ngimg = cv2.imread(\u201ccat.jpg\u201d,0)\r\n\r\n#Displaying images\r\ncv2.imshow(\u201ccat image\u201d, img)\r\ncv2.imshow(\u201cgray image\u201d, gimg)\r\n\r\ncv2.waitKey(0)\r\ncv2.destroyAllWindows()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74477\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv.png\" alt=\"read, display and save image in opencv\" width=\"850\" height=\"382\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv.png 850w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv-150x67.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv-300x135.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv-768x345.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/display-image-in-opencv-520x234.png 520w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/a><\/p>\n<p>The function has two parameters. The first parameter is a string that describes the name of the window. The second parameter is the image array, which contains the image data.<\/p>\n<p>imshow pops up a window, waitKey pauses until a key is pressed, and destroyAllWindows cleans up. These three steps form the \u201cdisplay loop\u201d used in nearly every OpenCV tutorial.<\/p>\n<p><strong><code>cv2.waitKey()<\/code><\/strong> function waits for a keyboard interrupt for a specific amount of time in milliseconds. Here we passed 0 (zero), which specifies that it has to wait for an indefinite amount of time until we press any key from the keyboard, and the execution continues.<\/p>\n<p>The next function <strong><code>cv2.destroyAllWindows()<\/code><\/strong> Closes all the windows in which images are displayed.<\/p>\n<p><em><strong>Check the <a href=\"https:\/\/data-flair.training\/blogs\/opencv-features\/\">OpenCV Features<\/a> that you didn&#8217;t know about<\/strong><\/em><\/p>\n<h4>Writing Images using OpenCV<\/h4>\n<p>To save an image to your local disk, we have a function <strong><code>cv2.imwrite()<\/code><\/strong>.<\/p>\n<p><strong>The function has two arguments:<\/strong><\/p>\n<p>1. The first argument is a string that is the file name.<\/p>\n<p>2. The second argument is the image array that you want to save.<\/p>\n<p><strong><code>cv2.imwrite(\u201ccat_image.png\u201d, img)<\/code><\/strong><\/p>\n<h3>Summarizing Everything<\/h3>\n<p>So now you can read, display, and save images in <a href=\"https:\/\/en.wikipedia.org\/wiki\/OpenCV\">OpenCV<\/a>. Let&#8217;s make a program that displays an image, then waits for the keyboard interrupt. If the user presses the \u2018c\u2019 key, then it will save the color image to our disk; if the user hits the \u2018g\u2019 key, then it saves the image in grayscale.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import cv2\r\n\r\n#Reading images in color and grayscale\r\ncolor_img = cv2.imread('cat.jpeg')\r\ngray_img = cv2.imread('cat.jpeg',0)\r\n\r\n#Displaying the image\r\ncv2.imshow('Cat image', color_img)\r\n\r\n#Storing the key pressed by user \r\nk = cv2.waitKey(0)\r\n\r\n#Check if user hits \u2018c\u2019 or \u2018g\u2019 key\r\nif( k == ord('c') ):\r\n  cv2.imwrite('color.jpg', color_img )\r\n  print(\"Image is saved color\")\r\n  cv2.destroyAllWindows()\r\n\r\nif( k == ord('g') ):\r\n  cv2.imwrite('gray.jpg', gray_img )\r\n  print(\"Image saved in grayscale\")\r\n  cv2.destroyAllWindows()<\/pre>\n<h3>Summary<\/h3>\n<p>In this read, display image in OpenCV article, we learned how to read images and write images onto our local disk. We also saw how we can take input from the user and check which key the user has pressed. At last, we made a basic program in OpenCV on how to work with images.<\/p>\n<p><em><strong>Time to work on <a href=\"https:\/\/data-flair.training\/blogs\/python-projects-with-source-code\/\">Free Python Projects with Source Code<\/a><\/strong><\/em><\/p>\n<p><em>Any difficulty in reading, displaying, and writing images in OpenCV? Mention in the comment section.<\/em><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1223,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/OpenCV&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last tutorial, we successfully set up the OpenCV environment. Now we can finally start learning the basics. In this section, we\u2019ll learn more than just typing commands and start seeing actual results.&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":74567,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[36720,36721,21710,21709],"class_list":["post-74471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-how-to-display-image-in-opencv","tag-how-to-save-image-in-opencv","tag-opencv-save-image-python","tag-read-display-image-in-opencv"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Read, Display and Save Image in OpenCV - Step-by-step Guide - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn to read, save &amp; display images in OpenCV using functions &amp; source code in Python. Make a program to display image using OpenCV.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Read, Display and Save Image in OpenCV - Step-by-step Guide - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn to read, save &amp; display images in OpenCV using functions &amp; source code in Python. Make a program to display image using OpenCV.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-15T06:52:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T04:50:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/how-to-read-display-and-save-image-in-opencv.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Read, Display and Save Image in OpenCV - Step-by-step Guide - DataFlair","description":"Learn to read, save & display images in OpenCV using functions & source code in Python. Make a program to display image using OpenCV.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/","og_locale":"en_US","og_type":"article","og_title":"How to Read, Display and Save Image in OpenCV - Step-by-step Guide - DataFlair","og_description":"Learn to read, save & display images in OpenCV using functions & source code in Python. Make a program to display image using OpenCV.","og_url":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-01-15T06:52:04+00:00","article_modified_time":"2026-04-28T04:50:38+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/how-to-read-display-and-save-image-in-opencv.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"How to Read, Display and Save Image in OpenCV &#8211; Step-by-step Guide","datePublished":"2020-01-15T06:52:04+00:00","dateModified":"2026-04-28T04:50:38+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/"},"wordCount":594,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/how-to-read-display-and-save-image-in-opencv.jpg","keywords":["how to display image in opencv","how to save image in opencv","opencv save image python","read display image in opencv"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/","url":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/","name":"How to Read, Display and Save Image in OpenCV - Step-by-step Guide - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/how-to-read-display-and-save-image-in-opencv.jpg","datePublished":"2020-01-15T06:52:04+00:00","dateModified":"2026-04-28T04:50:38+00:00","description":"Learn to read, save & display images in OpenCV using functions & source code in Python. Make a program to display image using OpenCV.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/how-to-read-display-and-save-image-in-opencv.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/how-to-read-display-and-save-image-in-opencv.jpg","width":802,"height":420,"caption":"how to read, display and save image in opencv"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/read-display-save-image-opencv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"How to Read, Display and Save Image in OpenCV &#8211; Step-by-step Guide"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/74471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=74471"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/74471\/revisions"}],"predecessor-version":[{"id":147967,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/74471\/revisions\/147967"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/74567"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=74471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=74471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=74471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}