Simple question but I'm not able to find something out there...
Is there a simple and user friendly tool that can be used within a jupyter-notebook
to let the user draw something in black on a white space (let say of size (x,y) pixels) after running a cell?
The drawing has to be returned (or even temporarily saved) as an array/image which can then be used by numpy
for example.
Jupyter Notebooks have a range of widgets to make these kinds of changes more user-friendly, aesthetically pleasing, and intuitive. In addition to all these benefits, you will also learn UI concepts and test code that handles user input.
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.
IPython Magic – Timing These are especially handy when you have some slow code and you're trying to indentify where the issue is. %%time will give you information about a single run of the code in your cell.
The widget is a box containing a drawing pad (main widget) and buttons (related to functionalities described below). Here is the drawing pad. You can draw whatever you want and data will be synchronised with the Python.
Jupyter Notebook - Plotting. IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show () function of plot object is not called. The show () function causes the figure ...
Jupyter Notebooks have a range of widgets to make these kinds of changes more user-friendly, aesthetically pleasing, and intuitive. In addition to all these benefits, you will also learn UI concepts and test code that handles user input. Let’s explore some essential user inputs.
Data visualization enables you to find context for your data through maps or graphs. This tutorial offers an insightful guide to interacting with graphs in Jupyter Notebook.
The Jupyter user interfaces offer a foundation of interactive computing environments where scientific computing, data science, and analytics can be performed using a wide range of programming languages. Web-based application for authoring documents that combine live-code with narrative text, equations and visualizations. Documentation | Repo
you can do that using PIL
and tkinter
libraries, like:
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()
You can modify the save
function to read the image using PIL
and numpy
to have it as an numpy
array.
hope this helps!
Try ipycanvas with interactive drawing mode. It actually has a demo notebook with interactive drawing that could be easily modified to do exactly what you want. It also has numpy support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With