{"id":26731,"date":"2022-04-01T00:18:36","date_gmt":"2022-03-31T18:48:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=26731"},"modified":"2022-04-01T00:18:36","modified_gmt":"2022-03-31T18:48:36","slug":"how-to-convert-image-into-a-pencil-sketch-using-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/","title":{"rendered":"How to Convert Image into a Pencil Sketch Using Python"},"content":{"rendered":"

An image in Python is simply a two-dimensional array of integers. So, using several Python tools, one can perform a few matrix operations to obtain some quite amazing effects. To convert a regular image to a sketch, we will change its original RGB values and assign them RGB values equivalent to grey, resulting in a sketch of the input image.<\/p>\n

Here we use OpenCV\u00a0 and matplotlib modules(for plotting figures\/images)<\/p>\n

Converting Image into a Pencil Sketch Using Python<\/h2>\n

1)Importing Modules<\/strong><\/p>\n

# Import cv2(OpenCV) module using the import keyword\r\nimport cv2\r\n# Import pyplot of matplotlib module using the import keyword\r\nimport matplotlib.pyplot as plt\r\n# Use the seaborn library using use() function\r\nplt.style.use('seaborn')<\/pre>\n

2)Plotting the given Original Image<\/strong><\/p>\n

# Load some random image by passing the filepath\/name of the image to the imread() \r\n# function and store it in a variable\r\ngvn_image = cv2.imread(\"sampleimage.png\")\r\n# Change the color code of the given image to RGB format using the cvtColor() function\r\n# to get the original colors\r\ngvn_image = cv2.cvtColor(gvn_image, cv2.COLOR_BGR2RGB)\r\n# Pass some random image size to be plotted(width, length) to the figure() function\r\nplt.figure(figsize=(6,6))\r\n# Plot the given original image using the imshow() function\r\nplt.imshow(gvn_image)\r\nplt.axis(\"off\")\r\n# Give the Title of the image using the title() function\r\nplt.title(\"Given Sample Image\")\r\n# Display the plot(image) using the plot function\r\nplt.show()<\/pre>\n

Output:<\/strong><\/p>\n

\"Sample<\/p>\n

3)Convert the given image to the Grayscale<\/strong><\/p>\n

The cvtColor () function to used to convert the image to a grayscale image to reduce the image’s complexity and make it easier to work with.<\/p>\n

# Pass the given image, COLOR_BGR2GRAY as arguments to the cvtColor() function\r\n# to convert the given image to grayscale image\r\n# Store it in a variable\r\ngrayscle_image = cv2.cvtColor(gvn_image, cv2.COLOR_BGR2GRAY)\r\n# Pass some random image size to be plotted(width, length) to the figure() function\r\nplt.figure(figsize=(6,6))\r\n# Plot the above Grayscale image for the given image using the imshow() function\r\nplt.imshow(grayscle_image, cmap=\"gray\")\r\nplt.axis(\"off\")\r\n# Give the Title of the image using the title() function\r\nplt.title(\"Grayscale Image\")\r\n# Display the grayscale image using the plot function\r\nplt.show()<\/pre>\n

Output:<\/strong><\/p>\n

\"Converting<\/p>\n

4)Getting the Inverted Image<\/strong><\/p>\n

Inverting the image is the next process. Now you’re probably wondering why you’d do something like that. The reason is that inverting an image helps in the analysis of the image with greater precision and detail.<\/p>\n

For the sketch generation, a more detailed study is necessary because the sketches must be exact and good.<\/p>\n

# Get the inverted image of by passing the above grayscale image as an \r\n# argument to the bitwise_not() function\r\ninvertd_image = cv2.bitwise_not(grayscle_image)\r\nplt.figure(figsize=(6,6))\r\nplt.imshow(invertd_image, cmap=\"gray\")\r\nplt.axis(\"off\")\r\nplt.title(\"Inverted Image\")\r\nplt.show()<\/pre>\n

Output:<\/strong><\/p>\n

\"Getting<\/p>\n

5)Getting a Smoothened Image<\/strong><\/p>\n

we will smooth the image to ensure that the sketch we obtain is less edgy and smooth. The corresponding code is displayed below.<\/p>\n

# Get the Smoothened image from the above inverted image using the\r\n# GaussianBlur() function\r\nsmooth_image = cv2.GaussianBlur(invertd_image, (21, 21),sigmaX=0, sigmaY=0)\r\nplt.figure(figsize=(6, 6))\r\nplt.imshow(smooth_image, cmap=\"gray\")\r\nplt.axis(\"off\")\r\nplt.title(\"Smoothened Image using GaussianBlur() function\")\r\nplt.show()<\/pre>\n

Output:<\/strong><\/p>\n

\"Smoothened<\/p>\n

6)Getting a Pencil Sketch Image<\/strong><\/p>\n

Now that we’ve completed the image processing, we’ll provide the previous outputs to the function, together with the correct and precise parameters, to make the necessary alterations to the image.<\/p>\n

# Get the output pencil sktech image for the given image using the divide() function \r\n# by passing the above gray scale image, smoothened image, scale as arguments to it.\r\npencil_skecth = cv2.divide(grayscle_image, 255 - smooth_image, scale=255)\r\nplt.figure(figsize=(6, 6))\r\nplt.imshow(pencil_skecth, cmap=\"gray\")\r\nplt.axis(\"off\")\r\nplt.title(\"Output Pencil Sketch Image\")\r\nplt.show()<\/pre>\n

Output:<\/strong><\/p>\n

\"Final<\/p>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

An image in Python is simply a two-dimensional array of integers. So, using several Python tools, one can perform a few matrix operations to obtain some quite amazing effects. To convert a regular image to a sketch, we will change its original RGB values and assign them RGB values equivalent to grey, resulting in a …<\/p>\n

How to Convert Image into a Pencil Sketch Using Python<\/span> Read More »<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nHow to Convert Image into a Pencil Sketch Using Python - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Convert Image into a Pencil Sketch Using Python - Python Programs\" \/>\n<meta property=\"og:description\" content=\"An image in Python is simply a two-dimensional array of integers. So, using several Python tools, one can perform a few matrix operations to obtain some quite amazing effects. To convert a regular image to a sketch, we will change its original RGB values and assign them RGB values equivalent to grey, resulting in a … How to Convert Image into a Pencil Sketch Using Python Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-31T18:48:36+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vikram Chiluka\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#webpage\",\"url\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/\",\"name\":\"How to Convert Image into a Pencil Sketch Using Python - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2022-03-31T18:48:36+00:00\",\"dateModified\":\"2022-03-31T18:48:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Convert Image into a Pencil Sketch Using Python\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\"},\"headline\":\"How to Convert Image into a Pencil Sketch Using Python\",\"datePublished\":\"2022-03-31T18:48:36+00:00\",\"dateModified\":\"2022-03-31T18:48:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#webpage\"},\"wordCount\":256,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\",\"name\":\"Vikram Chiluka\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"caption\":\"Vikram Chiluka\"},\"url\":\"https:\/\/python-programs.com\/author\/vikram\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Convert Image into a Pencil Sketch Using Python - Python Programs","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:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Convert Image into a Pencil Sketch Using Python - Python Programs","og_description":"An image in Python is simply a two-dimensional array of integers. So, using several Python tools, one can perform a few matrix operations to obtain some quite amazing effects. To convert a regular image to a sketch, we will change its original RGB values and assign them RGB values equivalent to grey, resulting in a … How to Convert Image into a Pencil Sketch Using Python Read More »","og_url":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2022-03-31T18:48:36+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Vikram Chiluka","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#webpage","url":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/","name":"How to Convert Image into a Pencil Sketch Using Python - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2022-03-31T18:48:36+00:00","dateModified":"2022-03-31T18:48:36+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"How to Convert Image into a Pencil Sketch Using Python"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602"},"headline":"How to Convert Image into a Pencil Sketch Using Python","datePublished":"2022-03-31T18:48:36+00:00","dateModified":"2022-03-31T18:48:36+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#webpage"},"wordCount":256,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/how-to-convert-image-into-a-pencil-sketch-using-python\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602","name":"Vikram Chiluka","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","caption":"Vikram Chiluka"},"url":"https:\/\/python-programs.com\/author\/vikram\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/26731"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=26731"}],"version-history":[{"count":13,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/26731\/revisions"}],"predecessor-version":[{"id":26803,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/26731\/revisions\/26803"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=26731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=26731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=26731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}