python - 在 Tkinter 中沿画布重置图像
问题描述
我已使用此答案中的代码来绘制和保存图像。此外,对于我的具体情况,我需要一个重置操作/按钮,它会重置画布,以便用户可以绘制新图像并保存它。不幸的是,它没有做我想要的,因为它只是与以前的图像重叠,即使画布被重置。我知道图像也应该重置,但我不知道如何实现。这是我修改后的代码:
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()
我正在尝试使用以下功能达到重置画布和图像的目标:
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()
推荐阅读
- php - Laravel 请求验证仅在用户 id 与记录的相同时才允许
- python - 将字典列表转换为列表字典
- python - Flask wtf 表单不验证自定义验证器
- r - R - 使用pdftools检索文本时出错(错误:poppler_pdf_text中的错误(loadfile(pdf),opw,upw):空间不足)
- flutter - Flutter - 自动向下滚动
- amazon-dynamodb - VTL:向数组内的地图添加新属性(AWS Appsync、DynamoDB)
- asp.net-core - Razor 页面:复杂的建模绑定不更新模型
- ruby-on-rails - 在一个迁移 Rails 中添加、填充表格列
- emeditor - 如何在 Emeditor 中进行负列搜索?
- typescript - 如何在 Typescript 中创建 UUID 模板文字类型?