在 Tkinter 中沿 canvas 重置图像
Resetting image along canvas in Tkinter
我已经使用此 answer 中的代码来绘制和保存图像。此外,对于我的具体情况,我需要重置 action/button,这将重置 canvas,以便用户可以绘制新图像并保存。不幸的是,它并没有达到我的要求,因为它只是与之前的图像重叠,即使 canvas 已重置。我知道图像也应该重置,但我不知道如何实现。
这是我修改后的代码:
from PIL import ImageTk, Image, ImageDraw
import PIL
from tkinter import *
import os
import numpy as np
width = 900
height = 900
white = (255, 255, 255)
green = (0, 128, 128)
log_max = 150 # maximum values
stick_step = 10 # sticks step on the axis
path_temp = "C:/Users/<User>/<Folder>"
def save():
global image_number
filename = path_temp + "/" + "template.png"
image1.save(filename)
def activate_paint(e):
global lastx, lasty
cv.bind('<B1-Motion>', paint)
lastx, lasty = e.x, e.y
def paint(e):
global lastx, lasty
x, y = e.x, e.y
myLine = cv.create_line((lastx, lasty, x, y), width=5)
# --- PIL
draw.line((lastx, lasty, x, y), fill='black', width=5)
lastx, lasty = x, y
return myLine
def clear():
cv.delete('all')
draw_sticks()
# initialize Tkinter window
root = Tk()
root.title("Template")
# coordinates during drawing
lastx, lasty = None, None
# rectangular area intended for drawing a log of width and height similar to those of the log images with white background
cv = Canvas(root, width=width, height=height, bg='white')
# case specific axis generation
def draw_sticks():
# adding the line representing axis of a log
horiz_position_of_axis = width
cv.create_line(0, horiz_position_of_axis, width, horiz_position_of_axis, fill="#476042", width=3)
# add log assisting scale for the user
image_step = int(width*stick_step/log_max)
# adding a first stick and value as a text
cv.create_text(10, horiz_position_of_axis-20, font=("Calibri", 7), text="0")
cv.create_line(3, horiz_position_of_axis-10, 3, horiz_position_of_axis, fill="#476042", width=2)
# adding other sticks
for i in range(int(log_max/stick_step)):
stick_x_pos = (image_step*(i+1))
cv.create_line(stick_x_pos, horiz_position_of_axis-10, stick_x_pos, horiz_position_of_axis, fill="#476042", width=2)
if stick_x_pos!=width:
text = str(int(stick_x_pos*log_max/width))
cv.create_text(stick_x_pos, horiz_position_of_axis-20, font=("Calibri", 7), text=text)
else:
cv.create_text(width-10, horiz_position_of_axis-20, font=("Calibri", 7), text="150")
image1 = PIL.Image.new('RGB', (width, height), 'white')
draw = ImageDraw.Draw(image1)
def draw_save():
draw_sticks()
# bind the activate_paint function, which in turn activates paint function
cv.bind('<1>', activate_paint)
cv.pack(expand=YES, fill=BOTH)
# adding "Save" button
btn_save = Button(text="save", command=save)
btn_save.pack()
# reset=Button(text='Reset canvas',command=clear)
# reset.pack(side=LEFT)
root.mainloop()
# read the image as a grayscale array
filename = os.listdir(path_temp)
img = PIL.Image.open(path_temp + "/" + filename[0]).convert('LA')
template_gray = img.convert('L')
return template_gray
if __name__ == "__main__":
draw_save()
我正在尝试使用以下函数达到重置 canvas 和图像的目标:
def clear():
cv.delete('all')
draw_sticks()
要重置 PIL 图片,您只需绘制一个与图片大小相同的白色矩形即可:
def clear():
draw.rectangle((0, 0, width, height), width=0, fill='white')
cv.delete('all')
draw_sticks()
我已经使用此 answer 中的代码来绘制和保存图像。此外,对于我的具体情况,我需要重置 action/button,这将重置 canvas,以便用户可以绘制新图像并保存。不幸的是,它并没有达到我的要求,因为它只是与之前的图像重叠,即使 canvas 已重置。我知道图像也应该重置,但我不知道如何实现。 这是我修改后的代码:
from PIL import ImageTk, Image, ImageDraw
import PIL
from tkinter import *
import os
import numpy as np
width = 900
height = 900
white = (255, 255, 255)
green = (0, 128, 128)
log_max = 150 # maximum values
stick_step = 10 # sticks step on the axis
path_temp = "C:/Users/<User>/<Folder>"
def save():
global image_number
filename = path_temp + "/" + "template.png"
image1.save(filename)
def activate_paint(e):
global lastx, lasty
cv.bind('<B1-Motion>', paint)
lastx, lasty = e.x, e.y
def paint(e):
global lastx, lasty
x, y = e.x, e.y
myLine = cv.create_line((lastx, lasty, x, y), width=5)
# --- PIL
draw.line((lastx, lasty, x, y), fill='black', width=5)
lastx, lasty = x, y
return myLine
def clear():
cv.delete('all')
draw_sticks()
# initialize Tkinter window
root = Tk()
root.title("Template")
# coordinates during drawing
lastx, lasty = None, None
# rectangular area intended for drawing a log of width and height similar to those of the log images with white background
cv = Canvas(root, width=width, height=height, bg='white')
# case specific axis generation
def draw_sticks():
# adding the line representing axis of a log
horiz_position_of_axis = width
cv.create_line(0, horiz_position_of_axis, width, horiz_position_of_axis, fill="#476042", width=3)
# add log assisting scale for the user
image_step = int(width*stick_step/log_max)
# adding a first stick and value as a text
cv.create_text(10, horiz_position_of_axis-20, font=("Calibri", 7), text="0")
cv.create_line(3, horiz_position_of_axis-10, 3, horiz_position_of_axis, fill="#476042", width=2)
# adding other sticks
for i in range(int(log_max/stick_step)):
stick_x_pos = (image_step*(i+1))
cv.create_line(stick_x_pos, horiz_position_of_axis-10, stick_x_pos, horiz_position_of_axis, fill="#476042", width=2)
if stick_x_pos!=width:
text = str(int(stick_x_pos*log_max/width))
cv.create_text(stick_x_pos, horiz_position_of_axis-20, font=("Calibri", 7), text=text)
else:
cv.create_text(width-10, horiz_position_of_axis-20, font=("Calibri", 7), text="150")
image1 = PIL.Image.new('RGB', (width, height), 'white')
draw = ImageDraw.Draw(image1)
def draw_save():
draw_sticks()
# bind the activate_paint function, which in turn activates paint function
cv.bind('<1>', activate_paint)
cv.pack(expand=YES, fill=BOTH)
# adding "Save" button
btn_save = Button(text="save", command=save)
btn_save.pack()
# reset=Button(text='Reset canvas',command=clear)
# reset.pack(side=LEFT)
root.mainloop()
# read the image as a grayscale array
filename = os.listdir(path_temp)
img = PIL.Image.open(path_temp + "/" + filename[0]).convert('LA')
template_gray = img.convert('L')
return template_gray
if __name__ == "__main__":
draw_save()
我正在尝试使用以下函数达到重置 canvas 和图像的目标:
def clear():
cv.delete('all')
draw_sticks()
要重置 PIL 图片,您只需绘制一个与图片大小相同的白色矩形即可:
def clear():
draw.rectangle((0, 0, width, height), width=0, fill='white')
cv.delete('all')
draw_sticks()