{"id":18510,"date":"2021-06-22T07:41:27","date_gmt":"2021-06-22T07:41:27","guid":{"rendered":"https:\/\/www.askpython.com\/?p=18510"},"modified":"2021-06-22T17:09:19","modified_gmt":"2021-06-22T17:09:19","slug":"opencv-puttext","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/opencv-puttext","title":{"rendered":"OpenCV putText() &#8211; Writing Text on Images"},"content":{"rendered":"\n<p>Hello fellow learner! In this tutorial, we will learn how to write string text on Images in Python using the OpenCV putText() method. So let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is the OpenCV putText() method?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/read-images-in-python-opencv\" data-type=\"post\" data-id=\"8838\">OpenCV<\/a> Python is a library of programming functions mainly aimed at real-time computer vision and <a href=\"https:\/\/www.askpython.com\/python\/examples\/image-processing-in-python\" data-type=\"post\" data-id=\"9154\">image processing<\/a> problems. <\/p>\n\n\n\n<p>OpenCV contains <code>putText()<\/code> method which is used to put text on any image. The method uses following parameters.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><em>img:<\/em><\/strong> The Image on which you want to write the text.<\/li><li><strong><em>text:<\/em><\/strong> The text you want to write on the image.<\/li><li><strong><em>org:<\/em><\/strong> It is the coordinates of the Bottom-Left corner of your text. It is represented as a tuple of 2 values (X, Y). X represents the distance from the left edge and Y represents the distance from the top edge of the image.<\/li><li><strong><em>fontFace:<\/em><\/strong> It denotes the type of font you want to use. OpenCV supports only a subset of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hershey_fonts\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Hershey Fonts<\/a>.<ul><li>FONT_HERSHEY_SIMPLEX<\/li><li>FONT_HERSHEY_PLAIN<\/li><li>FONT_HERSHEY_DUPLEX<\/li><li>FONT_HERSHEY_COMPLEX<\/li><li>FONT_HERSHEY_TRIPLEX<\/li><li>FONT_HERSHEY_COMPLEX_SMALL\u00a0<\/li><li>FONT_HERSHEY_SCRIPT_SIMPLEX<\/li><li>FONT_HERSHEY_SCRIPT_COMPLEX<\/li><li>FONT_ITALIC<\/li><\/ul><\/li><li><strong><em>fontScale:<\/em><\/strong> It is used to increase\/decrease the size of your text. The font scale factor is multiplied by the font-specific base size.<\/li><li><strong><em>color:<\/em><\/strong> It represents the color of the text that you want to give. It takes the value in <code>BGR<\/code> format, i.e., first blue color value, then green color value, and the red color value all in range 0 to 255.<\/li><li><strong><em>thickness (Optional):<\/em><\/strong> It represents the thickness of the lines used to draw a text. The default value is 1.<\/li><li><strong><em>lineType (Optional):<\/em><\/strong> It denotes the type of line you want to use. 4 <a href=\"https:\/\/docs.opencv.org\/3.4\/d0\/de1\/group__core.html#gaf076ef45de481ac96e0ab3dc2c29a777\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">LineTypes<\/a> available are<ul><li>FILLED\u00a0<\/li><li>LINE_4\u00a0<\/li><li>LINE_8\u00a0(Default)<\/li><li>LINE_AA<\/li><\/ul><\/li><li><strong><em>bottomLeftOrigin (Optional):<\/em><\/strong> When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner. The default value is False.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Adding text on an image using OpenCV &#8211; cv2.putText() method<\/h2>\n\n\n\n<p>Lets use the below image to write a &#8220;Good Morning&#8221; message using OpenCV putText() method.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Initial-Image.jpg\" alt=\"OpenCV PutText Initial Image\" class=\"wp-image-18512\" width=\"632\" height=\"395\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Initial-Image.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Initial-Image-300x188.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Initial-Image-768x480.jpg 768w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><figcaption>OpenCV PutText Initial Image<\/figcaption><\/figure><\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# importing cv2 library\nimport cv2\n\n# Reading the image\nimage = cv2.imread(&quot;Wallpaper.jpg&quot;)\n\n# Using cv2.putText()\nnew_image = cv2.putText(\n  img = image,\n  text = &quot;Good Morning&quot;,\n  org = (200, 200),\n  fontFace = cv2.FONT_HERSHEY_DUPLEX,\n  fontScale = 3.0,\n  color = (125, 246, 55),\n  thickness = 3\n)\n\n# Saving the new image\ncv2.imwrite(&quot;New Wallpaper.jpg&quot;, new_image)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Final-Image.jpg\" alt=\"OpenCV PutText Final Image\" class=\"wp-image-18513\" width=\"635\" height=\"398\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Final-Image.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Final-Image-300x188.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/OpenCV-putText-Final-Image-768x480.jpg 768w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><figcaption>OpenCV PutText Final Image<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, you learned about how to use OpenCV putText() method to write text on image. Thanks for reading!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello fellow learner! In this tutorial, we will learn how to write string text on Images in Python using the OpenCV putText() method. So let&#8217;s get started. What is the OpenCV putText() method? OpenCV Python is a library of programming functions mainly aimed at real-time computer vision and image processing problems. OpenCV contains putText() method [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":18516,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-18510","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=18510"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18510\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/18516"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=18510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=18510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=18510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}