在Tkinter中沿画布重置图像 [英] Resetting image along canvas in Tkinter

查看:49
本文介绍了在Tkinter中沿画布重置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了

I have used the code from this answer to draw and save images. In addition for my specific case, I needed a reset action/button, which would reset the canvas, so that a user can draw a new image and save it. Unfortunately, it doesn't do what I want, as it just overlaps the previous images, even though the canvas was reset. I understand that the image should be reset as well, but I don't know how to implement that. Here is my modified code:

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()

我正在尝试通过以下功能实现重置画布和图像的目的:

I am trying to reach the goal of resetting the canvas and image with the following function:

def clear():
    cv.delete('all')
    draw_sticks()

推荐答案

要重置PIL图像,您只需绘制一个与图像大小相同的白色矩形即可.

To reset the PIL image, you can just draw a white rectangle the size of the image:

def clear():
    draw.rectangle((0, 0, width, height), width=0, fill='white')
    cv.delete('all')
    draw_sticks()

这篇关于在Tkinter中沿画布重置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆