DEV Community

Cover image for Convert a image to sketch using python
Saurabh Chavan
Saurabh Chavan

Posted on • Edited on

1

Convert a image to sketch using python

image-to-sketch-python
in this tutorial we will create a intresting project which is how to convert a image into a sketch

let's get start...... :)
modules required for the project..
1.numpy
to install this modules you
have to use the command:)=
pip install numpy
2.imageio
installation:)=
pip install imageio
3.opencv
installation:)=
pip install opencv-python

let's go the editor i.e.VSCode

    #import the libraries
     import numpy as np
     import imageio
     import scipy.ndimage
     import cv2

     #lets create a variable 
      which   
      will store a image

     img="my.jpg"

    def grayscale(rgb):
     return 
        np.dot(rgb[...,:3],. 
     [0.299,0.587,0.114]) 
    #this is the formuleto 
    convert an image to black 
    and white image

    #this function will 
     convert your image into 
     sketch formate


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

   s=imageio.imread("my.jpg")
   g=grayscale(s)
   i=255-g

   #lets create blurred image

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

  #write the name of the 
  picture which you have to 
    want
  cv2.imwrite
  ("my_sketch.png",r)
Enter fullscreen mode Exit fullscreen mode

https://youtu.be/cws4_6yRSTo

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more