Skip to main content

Draw anyone's Sketch using Python Turtle Graphics in Python Programming

Draw anyone's Sketch using Python Turtle Graphics in Python Programming 👨‍💻🔥

In this post ,I will tell you about How to make Anyone's Sketch in Python using svg file and using these libraries: Turtle,cv2(opencv-python),svgpathtools,svg.path and tqdm.

You can install these libraries using pip command.

Turtle is pre-installed in mostly all programming softwares.

And here are the pip commands to install other libraries :

pip install opencv-python

pip install svgpathtools

pip install svg.path

pip install tqdm


𝗦𝘁𝗲𝗽𝘀 𝘁𝗼 𝗺𝗮𝗸𝗲 𝘀𝗸𝗲𝘁𝗰𝗵 : 

1)Go to https://svgconvert.com/#/ 

2)Upload the image and change the threshold value according to the image and download it as a svg file. 

3)Save the code and svg file in the same folder. 

Source Code :


import turtle as tu
import cv2
from svgpathtools import svg2paths2
from svg.path import parse_path
from tqdm import tqdm
class sketch_from_svg:

    def __init__(self,path,scale=30,x_offset=400,y_offset=400):

        self.path = path
        self.x_offset = x_offset
        self.y_offset = y_offset
        self.scale = scale

    def hex_to_rgb(self,string):
        strlen = len(string)
        if string.startswith('#'):
            if strlen == 7:
                r = string[1:3]
                g = string[3:5]
                b = string[5:7]
            elif strlen == 4:
                r = string[1:2]*2
                g = string[2:3]*2
                b = string[3:4]*2
        elif strlen == 3:
                r = string[0:1]*2
                g = string[1:2]*2
                b = string[2:3]*2
        else:
            r = string[0:2]
            g = string[2:4]
            b = string[4:6]
        
        return int(r,16)/255,int(g,16)/255, int(b,16)/255

    

    def load_svg(self):
        print('loading data')
        paths,attributes,svg_att = svg2paths2(self.path)
        h = svg_att["height"]
        w = svg_att['width']
        self.height = int(h[:h.find('.')])
        self.width = int(w[:w.find('.')])

        res = []
        for i in tqdm(attributes):
            path = parse_path(i['d'])
            co = i['fill']
            #print(co)
            col = self.hex_to_rgb(co)
            #print(col)
            n = len(list(path))+2       
            pts = [((int((p.real/self.width)*self.scale))-self.x_offset, (int((p.imag/self.height)*self.scale))-self.y_offset) for p in (path.point(i/n) for i in range(0,n+1))]
            res.append((pts,col))
            #res.append(pts)
        print('svg data loaded')
        return res

    def move_to(self,x, y):
        self.pen.up()
        self.pen.goto(x,y)
        self.pen.down()


    def draw(self,retain=True):
        coordinates = self.load_svg()
        self.pen = tu.Turtle()
        self.pen.speed(0)
        for path_col in coordinates:
            f = 1
            self.pen.color('black')
            #print(path_col)
            path = path_col[0]
            #print(path_col)
            col = path_col[1]
            #print(col)
            self.pen.color(col)
            self.pen.begin_fill()
            next = 0
            for coord in path:
                #for coord in path_col:
                x,y = coord
                y *= -1
                #print(x,y)
                if f:
                    self.move_to(x, y)
                    f=0
                else:
                    self.pen.goto(x,y)
            self.pen.end_fill()

        if retain == True:
            tu.done()
pen= sketch_from_svg('filename.svg',scale=80)
pen.draw()
Note : Change the filename in line 96 according to your filename and save the code and svg file in same folder to run the code.

Output Example : 

MS Dhoni Sketch by using this Code


If you like this post, Subscribe my YouTube Channel called "Vast Coding".
Click to redirect.

Your Queries:
netflix logo python code
netflix logo using python
netflix logo in python
netflix logo code python
python code for netflix logo
python netflix logo
python turtle graphics tutorial
python turtle graphics design
cool python turtle graphics
python turtle graphics game

Comments

  1. WOW... Bro It was superb...... May I know how you calculated the coordinates?

    ReplyDelete
    Replies
    1. There is a code in python which can trace coordinates of any image,it will be uploaded soon.

      Delete
  2. Sir please help me with this error

    File "d:\try\try.py", line 97, in
    pen.draw()
    File "d:\try\try.py", line 68, in draw
    coordinates = self.load_svg()
    File "d:\try\try.py", line 50, in load_svg
    co = i['fill']
    KeyError: 'fill'

    ReplyDelete

Post a Comment

Popular posts from this blog

Robert Downey Jr - Tony Stark - Ironman Sketch using Python Turtle Graphics

Robert Downey Jr Sketch using Python Turtle Graphics 👨‍💻 About Python Turtle Graphics : Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967. Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle , give it the command turtle. Note: If you are facing any issue to copy the code then copy the full page clicking 'select all',paste it to your notepad and copy the code from there. Here is the source code : import turtle as tu class rdj: def __init__(self): self.mouth = [(374, 382),(368, 382),(359, 382),(347, 379),(339, 381),(334, 384),(323, 380),(315, 380),(293, 385),(321, 387),(339, 387),(345, 386),(357, 385),(374, 383),(-1, -1),(389, 389),(393, 387),(395, 381),(395, 373),(377, 362),(366, 357),(358, 360),(351, 357),(345, 354),(344, 357),(337, 355),(333, 357),(327, 355),(323, 357),(303, 362),(

Sketchpy Python Library | How to use and How to Install

How to use sketchpy library in python | How to install sketchpy library in python programming Hello visitors, In this blogpost I will tell you about new python library called ' Sketchpy ' It's a new library in python programming. You can consider sketchpy as beginning level python project. What is sketchpy? Sketchpy is a library which allows you to draw any sketch using turtle graphics. In sketchpy library there are several classes and you can import these classes and then draw Rdj,Thalapathy Vijay,Apj Abdul kalam ,etc. This library also contains very useful codes like image coordinates tracer ,etc Sketchpy is a beginning level python project to draw amazing and awesome animations,drawings,designs using python turtle graphics. Usage ⬤  Install the package using    pip install sketchpy ⬤  Import it as import sketchpy in your project How to Install ⬤   Install using Terminal ⬤    Install using CMD {Command Prompt} Install pip install sketchpy It will definetely work, bu