Pencil Sketch Using Python

Ever wanted to be sketched by an artist?

But that costs a filthy lot, right?

Python will sketch you for free—and not just once, but any number of your photographs.

Import the Required Library

import cv2

Purpose: This line imports the OpenCV library, which provides functions to process images.

You need to have OpenCV installed (pip install opencv-python).

Load the Image

image = cv2.imread('input_image.jpg')

Purpose: This line reads the input image from your file system. Replace 'input_image.jpg' with the path to your image file.

Explanation: The cv2.imread() function loads an image from the specified file. The image is loaded in BGR format by default.

Convert to Grayscale

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Purpose: Converts the original colored image into a grayscale image.

Explanation: The cv2.cvtColor() function is used to convert the color space of an image.

Here, it’s converting from BGR to grayscale, which is necessary for the sketch effect.

Invert the Grayscale Image

inverted_image = cv2.bitwise_not(gray_image)

Purpose: Inverts the grayscale image to create a negative of the image.

Explanation: The cv2.bitwise_not() function inverts the pixel values of the image. For example, black becomes white, and vice versa.

Blur the Inverted Image

blurred_image = cv2.GaussianBlur(inverted_image, (21, 21), sigmaX=0, sigmaY=0)

Purpose: Apply a Gaussian blur to the inverted image to soften the edges.

Explanation: cv2.GaussianBlur() applies a Gaussian filter to the image. The (21, 21) specifies the kernel size, which affects the degree of blurring.

Larger kernels produce more blur.

Invert the Blurred Image

inverted_blurred_image = cv2.bitwise_not(blurred_image)

Purpose: Invert the blurred image, creating a lightened effect.

Explanation: Similar to step 4, this inverts the blurred image to produce a lighter version, which will be combined with the grayscale image in the next step

Create the Pencil Sketch

sketch = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)

Purpose: Combine the grayscale image with the inverted blurred image to create the pencil sketch effect.

Explanation: cv2.divide() performs element-wise division of the grayscale image by the inverted blurred image, enhancing the contrast to mimic a pencil sketch.

The scale=256.0 helps normalize the resulting image.

Save and Display the Sketch

cv2.imwrite('output_sketch.png', sketch)
cv2.imshow('Pencil Sketch', sketch)
cv2.waitKey(0)
cv2.destroyAllWindows()

Purpose: Save the final sketch image and display it in a window.

Explanation: cv2.imwrite() saves the sketch image to your disk. cv2.imshow() displays the image in a window. cv2.waitKey(0) waits for a key press, and cv2.destroyAllWindows() closes all OpenCV windows.

Full Source Code: Pencil Sketch Using Python

Click Here to View the Full Code
import cv2

# 1. Load the Image
image = cv2.imread('input_image.jpg')

# 2. Convert to Grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 3. Invert the Grayscale Image
inverted_image = cv2.bitwise_not(gray_image)

# 4. Blur the Inverted Image
blurred_image = cv2.GaussianBlur(inverted_image, (21, 21), sigmaX=0, sigmaY=0)

# 5. Invert the Blurred Image
inverted_blurred_image = cv2.bitwise_not(blurred_image)

# 6. Create the Pencil Sketch
sketch = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)

# 7. Save and Display the Sketch
cv2.imwrite('output_sketch.png', sketch)
cv2.imshow('Pencil Sketch', sketch)
cv2.waitKey(0)
cv2.destroyAllWindows()

Sample Output

input imagePencil Sketch Using Python

Leave a Reply