How to Convert Image to Pencil Sketch in Python?

I’m going to show you how you can convert any image into a pencil sketch using the Python programming language. This is a great way to get started with computer vision and image processing, and it’s also a lot of fun! Keep reading to find out how.

First, we’ll need to import the necessary libraries. We’ll be using the OpenCV and NumPy libraries for this tutorial. If you don’t have these installed, you can install them using pip:

pip install opencv-python pip install numpy

Next, we’ll load the image that we want to convert. You can use any image you like, but for this example, I’m going to use a picture of a flower:

import cv2 import numpy as np img = cv2.imread('flower.jpg')
Code language: JavaScript (javascript)

Now, we’ll convert the image to grayscale. This is an important step because it makes it easier for the algorithm to process the image:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

After that, we’ll apply some Gaussian blur to the image. This helps reduce noise and makes the pencil sketch effect more pronounced:

blur = cv2.GaussianBlur(gray, (5, 5), 0)

Now we’re ready to convert the image into a pencil sketch! There are several ways to do this, but for this tutorial, we’re going to use the Canny edge detector algorithm:

edges = cv2.Canny(blur, 10, 70)

All that’s left to do now is invert the image so that the pencil sketch effect is more pronounced:

# Invert the image corners = np.where(edges > 0)[::-1] color[corners] = (0,) * len(color[corners]) plt.imshow(color)
Code language: Python (python)

And that’s it! You should now have a beautiful pencil sketch of your original image:

img_sketch = cv2.Canny(img_gray, 10, 70) # Convert image to cartoon plt.imshow(cv2.cvtColor(img_sketch, cv2.COLOR_BGR2RGB)) # Display cartoon # Display results
Code language: Python (python)

Image to Pencil-Sketch conversion Script

A full source code

import cv2 import numpy as np inputPATH = '' outputPATH = 'output.jpg' img = cv2.imread(inputPATH) scale_percent = 0.60 width = int(img.shape[1] * scale_percent) height = int(img.shape[0] * scale_percent) dim = (width, height) resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) kernel_sharpening = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) sharpened = cv2.filter2D(resized, -1, kernel_sharpening) gray = cv2.cvtColor(sharpened, cv2.COLOR_BGR2GRAY) inv = 255 - gray gauss = cv2.GaussianBlur(inv, ksize=(15, 15), sigmaX=0, sigmaY=0) def dodgeV2(image, mask): return cv2.divide(image, 255 - mask, scale=256) pencil_img = dodgeV2(gray, gauss) # cv2.imshow('resized', resized) # cv2.imshow('sharp', sharpened) # cv2.imshow('gray', gray) # cv2.imshow('inv', inv) # cv2.imshow('gauss', gauss) # cv2.imshow('pencil sketch', pencil_jc) cv2.imshow("Output", pencil_img) cv2.imwrite(outputPATH, pencil_img) cv2.waitKey(0)
Code language: Python (python)

How to run this python script

  • Put the image file in the same directory as the Python script.
  • InputPATH in the script needs to be set to the name of the image path.
  • Run the script
  • See the image called “output.jpg” that looks like a sketch made with a pencil.

That’s all there is to it! Converting images into pencil sketches using Python is really easy once you know how. I hope you found this helpful tutorial and that you’ll now be able to convert any images you like into beautiful pencil sketches! Thanks for reading!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts