Open In App

Sketchpy in Python

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a versatile language that provides a lot of frameworks. This versatile nature of Python makes it emerging in every field. In the present day, Python is mostly used in the field of machine learning and data science. In this article, we will learn about a new module in Python that is Sketchpy which helps the users to trace the sketch and draw the sketch.

What is Sketchpy Module in Python

The Sketchpy module is one of the modules in Python which helps users to draw images and it provides tools along with some functionalities which help to draw images easily and help us to trace the images easily. This module was mainly designed to work with SVG images. SVG stands for SCALABLE VECTOR GRAPHICS.

Installing Sketchpy Module

To use the Sketchpy module first check if there is Python in your command prompt or not. In order to check that just type python in your command prompt. As the Sketchpy module is in Python, we need to install it manually. we know that we can download the packages using the pip command. just type the below command in your command prompt in order to install the module Sketchpy.

Installing Sketchpy Module

it will install the package

The above command will install the package sketchpy and now you can import the module sketchpy and use it.

Python3




import sketchpy
#It returns all the functions in the sketchpy
print(dir(sketchpy))


Output:

[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__path__’, ‘__spec__’]

In order to know the functions that are there in a particular module in the python just type the dir() method as mentioned above. Now look at the functions that are already available in the module sketchpy

Built-in function in sketchpy

Now let’s look at the methods in the sketchpy . There are some of the sub modules in the sketchpy like library, canvas etc. There are some of the inbuilt sketches that are available in the library in the sketchpy. Now let’s draw a sample picture in the below code

Example 1

It draws an image with the help of python turle graphics. Similiarly there are some of the inbuilt sketches in the library submodule in the sketchpy module in python.

Python3




from sketchpy import library as gfg
img = gfg.rdj()
#Initalizing the image object
img.draw()


Output:

Sketchpy in Python

Image of Robert Downey Jr

Example 2

Similiarly there are some of the inbuilt sketches in the library submodule of sketchpy module in Python.

Python3




from sketchpy import library as gfg
img = gfg.tom_holland()
img.draw()


Output:

Sketchpy in Python

Image of Tom Holland

Draw Images from the SVG Format

Now as we have mentioned earlier the main use of the module is to work with svg images we can also draw the sketches with the help of the svg images. Now let us draw the images from the svg format with the help of the sketchpy which is available in the canvas sub module and we can also sketch a image using the canvas module. Let’s see the below code as example.

Example 4

The sketch_from_image() takes a parameter as a file path. We have to give the file path correctly. For those raw image is not working double the ‘/’ in the path i.e ‘//’ and unicode error will be resolved other errors pls specify. Here we have mentioned a threshold value in the draw method it is default argument. Based on the thresh hold value the image will be converted as per our wish.Generally the threshold value for the images will be ranging from 100 to 255 it is the most common range where the threshold values will be chosen.

Python3




from sketchpy import canvas as gfg
img = gfg.sketch_from_image("path")
# Here the threshold value is the parameter that is used based on the user's choice
img.draw(threshold=150)


Output:

Here, we drew a grey scaled image from the color image.

Sketchpy in Python

Grey color Output of GFG logo



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads