Skip to content

codehelping.com

Best Platform to learn AI & Coding

  • Home
  • Projects
  • Notes
  • Blog
  • Contact Us

Convert Image to Pencil Sketch Using Python OpenCV | Source Code

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

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.

Convert Image to Pencil Sketch Using Python OpenCV | Source Code

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

Working of Pencil Sketch in Python Library

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, We use steps in image processing.

The first step is converting the image to grayscale, which removes all color information and simplifies the visual data.

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.

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, 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

    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.

    Thank you for visiting Codehelping.com, for more such content: stay active.

    Post navigation

    ❮ Previous Post: How to Download YouTube Videos Using Python | CodeHelping
    Next Post: Build a Simple Weather App in Python | Weather Forecast ❯

    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!