简体   繁体   中英

Pencil drawing on a tkinter canvas

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.

在此处输入图像描述

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)

在此处输入图像描述

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM