How to convert an image into a sketch using Python

How to convert image into sketch effect with OpenCVimageio and numpy packages using Python?


          Have you ever admired how to convert image into sketch ? Yes, it is possible to create beautiful pencil sketches using some Python packages. Follow up here:
         Interesting image filter effects, such as a pencil sketch or a cartoonizer effect, do not have to be very computationally involved to look good. In fact, in order to create a beautiful black-and-white pencil sketch effect, all you essentially need is some blurring and two image blending techniques called dodging and burning.
Using OpenCVimageio and numpy in Python this is how an RGB color image can be converted into a pencil sketch in four simple steps:
  1. Convert the RGB color image to grayscale clip.
  2. Apply colour inversion in order to get Greyscale clip.
  3. Apply a Gaussian blur to the negative(Greyscale clip).
  4. Blend the grayscale image from step 1 with the blurred negative from step 3 using a color dodge.
  5. Plot it or save.

Step 1: Convert the color image to Grayscale.

         This is really easy to do with OpenCV. First of all, Import all packages for appropriate functions. Now, declare a image and in the same name you are declaring, the image file should be saved in respective path and also must be in same format (jpg,jpj,png). Images can be opened with cv2. Alternatively, you can pass an additional argument to cv2.imread that specifies the color mode in which to open the image. The colour mode is specified below with RGB indices given.
import cv2

img="photo.jpg"

def grayscale(rgb):
 return np.dot(rgb[...,:3],[0.299,0.587,0.114])

Step 2: Obtain a negative clip.

         A negative of the image can be obtained by “inverting” the grayscale value of every pixel. Since by default grayscale values are represented as integers in the range [0,255] (i.e., precision CV_8U), the “inverse” of a grayscale value x is simply 255-x
s=imageio.imread(img)
g=grayscale(s)
i=255-g

Step 3: Apply a Gaussian blur

         A Gaussian blur is an effective way to both reduce noise and reduce the amount of detail in an image (also called smoothing an image). The size of the Gaussian kernel can be passed to cv2.GaussianBlur as an optional argument ksize. If both sigmaX and sigmaY are set to zero, the width of the Gaussian kernel will be derived from ksize.
b=scipy.ndimage.filters.gaussian_filter(i,sigma=10)
r=dodge(b,g)

Step 4: Blend the grayscale image with the blurred negative

         Dodging and burning refer to techniques employed during the printing process in traditional photography.Dodging lightened an image, whereas burning darkened it. This is major step in creating a sketch of an image.
This is essentially dividing the grayscale (or channel) value of an image pixel A by the inverse of the mask pixel value B, while making sure that the resulting pixel value will be in the range [0,255] and that we do not divide by zero. We could translate this into a Python function that accepts two OpenCV matrices and returns the blended image:
def dodge(front,back):
 result=front*255/(255-back)
 result[result>255]=255
 result[back==255]=255
 return result.astype('uint8')

Step 5 : Plotting / Saving sketched Image.

        This is the final, Important and easiest step. Yet though, a small mistake or error in this can led to malfunction of whole code. The saving process is done so by putting your destination file’s name with .imwrite. This will automatically write and save your sketched image on the same location as of the RGB coloured image taken initially.
cv2.imwrite('1.png',r)

NOTE :

  1. Make sure that you have pip installed all those libraries for Python earlier itself. Else, it may lead to an error.
  2. If any error occurs while running or if you don’t get the sketch saved automatically on the described path, try running the code on code prompts by declaring the path of script file.
  3. You can vary the B&W intensity and extent of blurry effects by changing the RGB values while initializing those on the code.(part 2)
Below is the full code for the same.
import numpy as np
import imageio
import scipy.ndimage
import cv2

img="photo.jpg"

def grayscale(rgb):
 return np.dot(rgb[...,:3],[0.299,0.587,0.114])

def dodge(front,back):
 result=front*255/(255-back)
 result[result>255]=255
 result[back==255]=255
 return result.astype('uint8')



s=imageio.imread(img)
g=grayscale(s)
i=255-g

b=scipy.ndimage.filters.gaussian_filter(i,sigma=10)
r=dodge(b,g)

cv2.imwrite('photo_sketch.png',r)

RESULTS

** THE CONENTS HERE ARE ARCHIVES FROM  www.pythonpool.com  AND PYTHONPOOL OWNS ITS COPYRIGHT TOO.

Comments

Popular posts from this blog

MicroPython #3: PWM, LED Fade

MicroPython #2: Triggering I/O's