Home > Enterprise >  Pencil drawing on a tkinter canvas
Pencil drawing on a tkinter canvas

Time:05-31

I am trying to draw with a "pencilish" look on a tkinter canvas. I can draw on the canvas like how a marker draws, here is an image.

enter image description here

I would also like to be able to draw like in a "Pencilish" style, here is a picture to what I mean(pic from paint 3d)

enter image description here

I think it would be possible with numpy, PIL or Open CV2 but I have no idea on how to draw a pencil sketch. The only info I could get is how to convert the whole image to a pencil sketch.

CodePudding user response:

Since you did not provide a minimal reproducible example. The code is left as an exercise to you:

import tkinter as tk
import math
import random

RADIUS = 10
SEEDS = 10

def random_point(x,y):
    a = random_angle = 2*math.pi*random.random()
    r = random_radius= RADIUS*math.sqrt(random.random())
    random_x = r*math.cos(a) x
    random_y = r*math.sin(a) y
    return random_x,random_y

def paint(event):
    x = event.x
    y = event.y
    canvas = event.widget
    for i in range(SEEDS):
        random_x,random_y = random_point(x,y)
        canvas.create_line(random_x,random_y,random_x 1,random_y 1,fill='black')
    

root = tk.Tk()
cnvs = tk.Canvas(root)
cnvs.bind('<Motion>',paint)
cnvs.pack()
root.mainloop() 

Happy coding!

  • Related