My python is not scary . He says he’s not lending his fangs for a sketch instead tells me to open an editor on a Laptop .
“You can open it on phone too only if you’re a nerd.” — python the sacred pet
So without wasting much time let’s open the editor and don’t contribute to the python using it fangs.
Something like above should be opened an Editor to code in python.
Find one Good Image to start with.
Code Snippets walk through
#.............................................................#
# Aap Chronology Samjhiye...
# 1.Import the libraries needed
# 2.Know what you want to do
# 3.Do the working
# 4.save it bruh!#.............................................................#
Codecussion Started.
Step.1 -> Reading image using python library imageio
import imageio
my_image=imageio.imread(“nicky.jpg”)
Analysis.1 -> “You write your image in between these quotes” to store your image in myimage
Step.2 -> We go by removing the colors of image in one word we’ve to Gray scale the image
# since we are using numpy and it doesn't have any inbuilt function for grayscale I scratched a formulae for it below# Y= 0.299 R + 0.587 G + 0.114 B {see it's simply graying the image by adjusting the RGB :xD} you can change the values as how much gray you prefer.import numpy as np
def grayscale(rgb):
# nope it's not deaf spelled wrong here def means defination(this is how one defines functions in python)
return np.dot(rgb[...,:3],[0.299,0.587,0.114])
my_grayimage=grayscale(my_image)
# nothing gross just calling my_image to be gray_image
Analysis.2 -> nope that :3 is not a smiley face the line is returning a dot product of the image Matrix with the gray values we ought to give it.
Step.3 -> Inverting the image
my_invertedimage = 255-my_grayimage
Analysis.3 -> We can invert images simply by subtracting from 255, as grayscale images are 8 bit images or one can say they have a maximum of 256 tones.
Step.4 -> Blur the image (ah! since we stored the image as an ndarray we will have to take scipy library to help)
import scipy.ndimage
my_blurimage=scipy.ndimage.filters.gaussian_filter(my_invertedimage,sigma=80)
# changing the value of sigma changes how shady you want your image to be less value means more white and vice versa
Analysis.4 -> I scratched my head to find Gaussian filter the best to blur the image you can surely allow to find your filter but if you don’t want to scratch your head you can simply rely on my scratching .
Step.5 -> Dodge/Blend the image (This step we define the edges and merge the blur_image and grayscale_image to have the blend of a sketch)
def dodge(front,back):
# front and back positions which amoungst the two image would be on top and which would be behind
result=front*255/(255-back)
result[result>255]=255
result[back==255]=255
return result.astype('uint8')
# returning result astype('uint8') here means converting the ndarray matrix back to image form so can be displayed pixel by pixel
Analysis.5 -> Here we merged two versions of our image that we created above using matrix calculations of the image
my_sketch=dodge(my_blurimage,my_grayimage)
# This is the final call we make to our image to transform into sketch I promise.
Step.6 -> Saving the image (ah! you invested this far and wouldn’t want to save it ..oh1 c’mon)
import cv2
cv2.imwrite('nickyketch1.png',my_sketch)
Analysis.6 -> we use here cv2 to save our image in the nam we want pardon me i mean our sketch in the name we want with the function property imwrite(im gonna write :xD)
Huh! told you that was the last call. Enjoy your sketch.
You can trust me with promises :0
Codecussion Ended.
Have your sketch ready.
For some people this would be an easy task to do
For some its easy just to use an app to sketch
While some are skilled with brushes of their own.
For me its just an experience I have started to try my hands on writing on Medium with a jovial zeal to code the things . If you find anything useful here I will be glad to know.
You can find the Gist here
<script src=”https://gist.github.com/Nickhill28/222a9ad7839316548939bb31c3b8614c.js"></script>