【问题标题】:Python - Tkinter - Paint: How to Paint smoothly and save images with a different names?Python - Tkinter - Paint:如何流畅地绘制并以不同的名称保存图像?
【发布时间】:2018-09-03 09:09:16
【问题描述】:

我做了一个绘画程序,但我不能流畅地绘制并且每次都用不同的名称保存图像。请帮忙!

from tkinter import *
# by Canvas I can't save image, so i use PIL
import PIL
from PIL import Image, ImageDraw


def save():
    filename = 'image.png'
    image1.save(filename)

def paint(event):
    x1, y1 = (event.x), (event.y)
    x2, y2 = (event.x + 1), (event.y + 1)
    cv.create_oval((x1, y1, x2, y2), fill='black', width=10)
    #  --- PIL
    draw.line((x1, y1, x2, y2), fill='black', width=10)


root = Tk()

cv = Canvas(root, width=640, height=480, bg='white')
# --- PIL
image1 = PIL.Image.new('RGB', (640, 480), 'white')
draw = ImageDraw.Draw(image1)
# ---- 
cv.bind('<B1-Motion>', paint)
cv.pack(expand=YES, fill=BOTH)

btn_save = Button(text="save", command=save)
btn_save.pack()

root.mainloop()

【问题讨论】:

    标签: python-3.x tkinter python-imaging-library paint tkinter-canvas


    【解决方案1】:

    您可以使用连续绘制而不是绘制单独的小圆圈。
    下面的示例存储鼠标位置的最后一个值以绘制一条线到当前值。

    你需要点击,然后移动鼠标来绘制;松开点击停止。

    图像名称包含一个数字,每次保存时加 1;因此,您可以在绘制完整图片时保存所有中间图像。

    from tkinter import *
    import PIL
    from PIL import Image, ImageDraw
    
    
    def save():
        global image_number
        filename = f'image_{image_number}.png'   # image_number increments by 1 at every save
        image1.save(filename)
        image_number += 1
    
    
    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
        cv.create_line((lastx, lasty, x, y), width=1)
        #  --- PIL
        draw.line((lastx, lasty, x, y), fill='black', width=1)
        lastx, lasty = x, y
    
    
    root = Tk()
    
    lastx, lasty = None, None
    image_number = 0
    
    cv = Canvas(root, width=640, height=480, bg='white')
    # --- PIL
    image1 = PIL.Image.new('RGB', (640, 480), 'white')
    draw = ImageDraw.Draw(image1)
    
    cv.bind('<1>', activate_paint)
    cv.pack(expand=YES, fill=BOTH)
    
    btn_save = Button(text="save", command=save)
    btn_save.pack()
    
    root.mainloop()
    

    据说不亚于你的可怕,但线条是连续的......

    【讨论】:

    • 非常感谢!
    • 有没有办法在空白画布/图形上添加 x 轴?在我的情况下,我需要它作为参考,同时绘图。
    • 是的,你可以这样做;这很简单:cv.create_line(root, 0, 240, 640, 240)
    • 我还修改了您的代码并添加了重置画布选项,该选项会清除画布,但仍将所有绘图留在图像上。所以,当我点击保存按钮时,它会完全覆盖所有图纸。
    • 是的,OP 没有要求此功能,也没有实现。实施起来并不难;您需要在重置画布时创建一个新图像,因此图纸不会重叠。
    【解决方案2】:

    如果要将图像另存为其他名称和扩展名,也可以使用另存为对话框:

    from tkinter import *
    from tkinter.filedialog import asksaveasfilename as saveAs
    import PIL
    from PIL import Image, ImageDraw
    
    def save():
        filename=saveAs(title="Save image as...",filetype=(("PNG images","*.png"),("JPEG images","*.jpg"),("GIF images","*.gif")))
        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
        cv.create_line((lastx, lasty, x, y), width=1)
    
        draw.line((lastx, lasty, x, y), fill='black', width=1)
        lastx, lasty = x, y
    def clear():
        cv.delete('all')
    def exitt():
        exit()
    
    win = Tk()
    win.title("Paint - made in Python")
    win.iconbitmap('Paint.ico') # Delete this line if you don't have file "Paint.ico" in this folder
    lastx, lasty = None, None
    
    cv = Canvas(win, width=640, height=480, bg='white')
    image1 = PIL.Image.new('RGB', (640, 480), 'white')
    draw = ImageDraw.Draw(image1)
    
    cv.bind('<1>', activate_paint)
    cv.pack(expand=YES, fill=BOTH)
    
    save_ = Button(text="Save image", command=save)
    save_.pack()
    
    reset=Button(text='Reset canvas',command=clear)
    reset.pack(side=LEFT)
    
    _exit=Button(text='Exit',command=exitt)
    _exit.pack(side=RIGHT)
    win.mainloop()
    

    Paint.pyw - Drawing window

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2016-08-22
      • 2023-03-14
      相关资源
      最近更新 更多