gpt4 book ai didi

python - Jupyter笔记本: let a user inputs a drawing

转载 作者:行者123 更新时间:2023-12-01 01:03:40 25 4
gpt4 key购买 nike

简单的问题,但我找不到任何东西......

是否有一个简单且用户友好的工具可以在 jupyter-notebook 中使用,让用户在白色空间上用黑色绘制一些东西(比如说大小(x,y)像素) )运行单元格后?

绘图必须以数组/图像的形式返回(甚至暂时保存),然后可供 numpy 使用。

最佳答案

您可以使用 PILtkinter 库来做到这一点,例如:

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 函数以使用 PILnumpy 读取图像,将其作为 numpy > 数组。希望这有帮助!

关于python - Jupyter笔记本: let a user inputs a drawing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55575685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com