一个简单的问题,但是我找不到东西...
是否有一个简单且用户友好的工具可以在a中使用,jupyter-notebook
以便让用户在运行单元格后在白色空间(以大小(x,y)像素为单位)上用黑色绘制一些东西?
绘图必须作为数组/图像返回(或什至临时保存),然后numpy
例如可以使用。
您可以使用PIL
和tkinter
库来实现,例如:
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()
您可以修改save
函数以使用读取图像PIL
并将numpy
其作为numpy
数组。希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句