我已經繪製在畫布上,然後保存我的圖紙圖像下面的代碼:獲取1x1px圖像保存帆布爲JPG時
def paint(event):
global brush_size
global color
x1 = event.x - brush_size
x2 = event.x + brush_size
y1 = event.y - brush_size
y2 = event.y + brush_size
w.create_oval(x1, y1, x2, y2,
fill = color,
outline=color)
def getter(widget):
x = root.winfo_rootx() + widget.winfo_x()
y = root.winfo_rooty() + widget.winfo_y()
x1 = x + widget.winfo_width()
y1 = y + widget.winfo_height()
ImageGrab.grab().crop((x,y,x1,y1)).save("img1.jpg")
root = Tk()
root.title("Paint")
w = Canvas(root,
width=canvas_width,
height=canvas_height,
bg="white")
w.bind("<B1-Motion>", paint)
save_btn = Button(text="Save", width=10, command=getter(w))
w.grid(row=2, column=0,
columnspan=7,
padx=5, pady=5,
sticky=E+W+S+N)
w.columnconfigure(6, weight=1)
w.rowconfigure(2, weight=1)
save_btn.grid(row=0, column=1)
root.mainloop()
但是當我點擊「保存」按鈕,我得到一個空1x1px JPG文件。你能告訴我這有什麼問題嗎?
P.S.還有一些額外的問題。當我快速繪畫的時候,我沒有得到一個持續的線條。取而代之的是間隙。我該如何解決?
請參閱http://stackoverflow.com/q/5767228/7432 –