Skip to content

codehelping.com

No. 1 For Project and Interview

  • Home
  • Projects
  • Blog
  • Contact Us

Convert Image to Pencil Sketch Using Python OpenCV | Source Code

Posted on July 16, 2025July 16, 2025 By Omkar Pathak No Comments on Convert Image to Pencil Sketch Using Python OpenCV | Source Code
Blog, Project, Python Project

In the world of digital art and image processing, converting a regular photograph into a pencil sketch is a fascinating task. It adds an artistic touch to your photos and can be used in design projects, social media content, or even app development.

This blog will walk you through how to convert an image to pencil sketch using Python, explain how the process works, and show you the complete code implementation.

Fortunately, with Python and the OpenCV library, achieving this effect is both simple and efficient.

Convert Image to Pencil Sketch Using Python OpenCV | Source Code

The Concept of Pencil Sketch using Python

Before jumping into the code, it’s important to understand how this transformation happens. When we think of a pencil sketch, we imagine a grayscale drawing with high contrast, primarily showing the outlines and textures of the image.

To replicate this digitally, we use several steps in image processing.

  1. The first step is converting the image to grayscale, which removes all color information and simplifies the visual data. This makes it easier to identify the primary edges and contours in the image.
  2. Next, we invert the grayscale image so that dark areas become light and light areas become dark. This is necessary for the next step, where we apply a Gaussian blur. Blurring the inverted image helps to smoothen the transitions and eliminate noise, giving it a more natural look.
  3. Once we have the blurred image, we invert it again and blend it with the original grayscale image using a technique called “dodge blending.”
    In OpenCV, this is done using the cv2.divide() function. This combination of operations results in an image that closely resembles a hand-drawn pencil sketch.

Code to Convert an Image to Pencil Sketch using python

Here is the complete Python script that performs the sketch transformation:

import cv2

# Load the image from the specified path
image1 = cv2.imread('E:\\demo.png')
window_name = 'Original image'
cv2.imshow(window_name, image1)

# Convert the image to grayscale
grey_img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# Invert the grayscale image
invert = cv2.bitwise_not(grey_img)

# Apply Gaussian Blur to the inverted image
blur = cv2.GaussianBlur(invert, (21, 21), 0)

# Invert the blurred image
invertedblur = cv2.bitwise_not(blur)

# Create the sketch using divide blend
sketch = cv2.divide(grey_img, invertedblur, scale=256.0)

# Save the result to a file
cv2.imwrite("E:\\sketch.png", sketch)

# Load and display the sketch image
image = cv2.imread("E:\\sketch.png")
window_name = 'Sketch image'
cv2.imshow(window_name, image)

# Wait for a key press and close all windows
cv2.waitKey(0)
cv2.destroyAllWindows()

This code reads an image, applies all the transformation steps explained earlier, and saves the final sketch image on your system. It also displays both the original and the sketch images in separate windows for visual comparison.

View this post on Instagram

A post shared by Code.hub (@code_helping)

Applications of Pencil Sketch Effects

This pencil sketch effect is more than just a fun transformation. It has practical applications in mobile photography apps, digital portfolios, educational projects, and even in automated image editing pipelines.

With some enhancements, this script can be integrated into a user interface, used for batch processing multiple images, or extended to apply other effects like color sketches or cartoon filters.

Conclusion

Image processing with Python and OpenCV opens up endless possibilities. Converting an image to pencil sketch using python library is a simple example, but it introduces you to powerful operations like color space transformations, image inversion, blurring, and pixel-wise blending. Understanding these basics will allow you to explore more advanced topics in computer vision.

This project is a great starting point for anyone interested in working with images or enhancing their Python skills through practical, visual applications. There are various such python projects here.

Try experimenting with different blur levels or image types and observe how the sketch quality changes. You’ll be surprised at how much control and creativity you can unlock with just a few lines of code.

Post navigation

❮ Previous Post: Download YouTube Videos Using Python – Step-by-Step Guide
Next Post: Resume Analyzer Project Web App: Match Resumes to Job Descriptions | Source Code ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact Us
  • Privacy Policy
  • Disclaimer

Copyright © 2025 codehelping.com.

Theme: Oceanly by ScriptsTown

Social Chat is free, download and try it now here!