Edit photo - Pencil sketch effect - Image processing in Python

Code:
from skimage import data, io, util
import random
import sys
from skimage.color import rgb2gray

try:
    rgb_image = data.imread(sys.argv[1])
   
    height = rgb_image.shape[0]
    width = rgb_image.shape[1]
   
    if(len(rgb_image.shape) == 3):
        rgb_image = util.img_as_ubyte(rgb2gray(rgb_image))
    for i in range(height):
        for j in range(width):
            if rgb_image[i,j] < 128:
                rgb_image[i,j] = 15
            else:
                rgb_image[i,j] = 240
   
    io.imsave("pencil_sketch.jpg", rgb_image)
   
except Exception as e:
    print(e)


Original Image (RGB):


Pencil Sketch:


Original Image (Grayscale):


Pencil Sketch:

This example produces a pencil sketch effect from a photo using scikit-image. The general technique converts the image to grayscale, inverts it, blurs the inversion, and blends it back to bring out edges like hand drawn strokes.

scikit-image is a practical, NumPy based toolkit for this kind of work, and the same building blocks of color conversion, filtering, and blending underlie many other photo effects.

Comments

Popular posts from this blog

The 2026 AI Cheat Sheet: Best Tool for Every Task

[Solved] Error: No such keg: /usr/local/Cellar/gcc