Convert Image into Sketch


In Python, an image is just a two-dimensional array of integers. So one can do a couple of matrix manipulations using various python modules in order to get some very interesting effects. In order to convert the normal image to a sketch, we will change its original RGB values and assign its RGB values similar to grey, in this way a sketch of the input image will be generated. 

Approach 1:

  1. Import all required modules (numpy, imageio, scipy.ndimage, OpenCV)
  2. Take Image input
  3. Check RGB value of image and convert into according to RGB values
  4. Show finale image output using cv2.imwrite()
  5.  ---------------------------------------------------------------------------------5

  1. code

  1. # Python program to Convert Image into sketch
  2. # import all the required modules
  3. import numpy as np
  4. import imageio
  5. import scipy.ndimage
  6. import cv2


  7. # take image input and assign variable to it
  8. img = "4.jpeg"


  9.    # function to convert image into sketch
  10.   def rgb2gray(rgb):
  11. # 2 dimensional array to convert image to sketch
  12. return np.dot(rgb[..., :3], [0.2989, 0.5870, .1140])


  13.     def dodge(front, back):

  14. # if image is greater than 255 (which is not possible) it will convert it to 255
  15. final_sketch = front*255/(255-back)
  16. final_sketch[final_sketch > 255] = 255
  17. final_sketch[back == 255] = 255

  18. # to convert any suitable existing column to categorical type we will use aspect function
  19. # and uint8 is for 8-bit signed integer
  20. return final_sketch.astype('uint8')


  21. ss = imageio.imread(img)
  22. gray = rgb2gray(ss)

  23. i = 255-gray


  24. # to convert into a blur image
  25. blur = scipy.ndimage.filters.gaussian_filter(i, sigma=13)


  26. # calling the function
  27. r = dodge(blur, gray)


  28. cv2.imwrite('4.png', r)

  29.      Approach 2:
  30.    Import cv2:

    --> pip install cv2
  31. Then we will import cv2 inside our code, after that, we will use some of the following functions: 

    1. imread()- This function will load the image i.e in the specified folder. 

    2. cvtColor()- This function takes color as an argument and then changes the source image color into that color.

    3. bitwise_not()- This function will help the image to keep the properties as same by providing the masking to it.

    4. GaussianBlur()- This function is used to modify the image by sharpening the edges of the image, smoothen the image, and will minimize the

    blurring property.

    5. divide()- This function is used for the normalization of the image as it doesn’t lose its previous properties.

    Finally will save the image using imwrite() function.

  32. import cv2

    image = cv2.imread('Image.jpg') # loads an image from the specified file

    # convert an image from one color space to another

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

    invert = cv2.bitwise_not(grey_img) # helps in masking of the image

    # sharp edges in images are smoothed while minimizing too much blurring

    blur = cv2.GaussianBlur(invert, (21, 21), 0)

    invertedblur = cv2.bitwise_not(blur)

    sketch = cv2.divide(grey_img, invertedblur, scale=256.0)

    cv2.imwrite("sketch.png", sketch) # converted image is saved as mentioned name

  33. Example 1:

    Input image:

  34. Output:




Post a Comment

0 Comments