Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Jupyter notebook: let a user inputs a drawing

from PIL import ImageTk, Image, ImageDraw
import PIL
from tkinter import *

width = 200  # canvas width
height = 200 # canvas height
center = height//2
white = (255, 255, 255) # canvas back

def save():
    # save image to hard drive
    filename = "user_input.jpg"
    output_image.save(filename)

def paint(event):
    x1, y1 = (event.x - 1), (event.y - 1)
    x2, y2 = (event.x + 1), (event.y + 1)
    canvas.create_oval(x1, y1, x2, y2, fill="black",width=5)
    draw.line([x1, y1, x2, y2],fill="black",width=5)

master = Tk()

# create a tkinter canvas to draw on
canvas = Canvas(master, width=width, height=height, bg='white')
canvas.pack()

# create an empty PIL image and draw object to draw on
output_image = PIL.Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(output_image)
canvas.pack(expand=YES, fill=BOTH)
canvas.bind("<B1-Motion>", paint)

# add a button to save the image
button=Button(text="save",command=save)
button.pack()

master.mainloop()
Comment

PREVIOUS NEXT
Code Example
Python :: element not found selenium stackoverflow 
Python :: remove too short strings from a list python 
Python :: print 1 thing repeatedly in 1 line python 
Python :: scikit learn ridge regression 
Python :: how to get input from user in python 
Python :: append to list in dictionary python if exists 
Python :: replacing values in pandas dataframe 
Python :: how to factorise expressions in python 
Python :: seasonal_decompose python 
Python :: python string to xml 
Python :: how to add column headers in pandas 
Python :: show pythonpath 
Python :: Slicing lexicographically pandas 
Python :: matplotlib title chopped off 
Python :: delete a record by id in flask sqlalchemy 
Python :: pickle load 
Python :: ax set xtick size 
Python :: web scraping linkedin profiles python jupyter 
Python :: exact distance math 
Python :: list to set keep order python 
Python :: how to visualize decision tree in python 
Python :: python text underline 
Python :: saving to csv without the index 
Python :: streamlit dropdown 
Python :: how to get the current url path in django template 
Python :: python pause 
Python :: dict to array of string python 
Python :: gpu training tensorflow 
Python :: chrome selenium python 
Python :: python print without space 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =