Python - Mouse as Pen & Save Image (tkinter , PIL)

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

That video is very useful for my project. Thank a lot

caotriinh
Автор

THE CODE >>> with updates.
I've added more notes, added a "show" button and a "clear" button, and a "load" button (not working)
I've also changed some things to better highlight that YOU DON'T SAVE THE TKINTER CANVAS. You save the unseen PIL canvas.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


from tkinter import *
import Image, ImageDraw, ImageShow

my_filename = "Change the file name here"
my_file_extension = ".png" # PIL image can be saved as .png .jpg .gif or .bmp file (among others)

width = 400
height = 300
center = height//2
white = (255, 255, 255)
red = (255, 0, 0)
image1 = None


def paint(event):
"""on event, draw oval onto Canvas (both)"""
x1, y1 = (event.x + 1), (event.y + 1)
x2, y2 = (event.x - 1), (event.y - 1)
cv.create_oval(x1, y1, x2, y2, fill="green", width=0) # On tkinter Canvas
draw.line([x1, y1, x2, y2], fill="red", width=0) # On PIL Canvas
# "fill" is the inside color. "Width" is the width of the outline (which is black by default.
# if we set "width" to 5 the outline would cover over the fill, and we would only see black.


def save():
"""save canvas image as filename"""
print("'save' button pressed")



def clear ():
"""clear the canvas"""
# Clear the SEEN canvas
cv.delete(ALL)
# Clear the UNSEEN canvas
"""Draws a white rectangle over the entire canvas. """
w = image1.width
h = image1.height
draw.rectangle([0, 0, w, h], fill="white", width=0) # On PIL Canvas

def load ():
print("'load' button pressed")
# I couldn't get this one to work. If you can then please comment below.

def show ():
print("'show' button pressed")
image1.show()

# root is the main window used in our GUI, it builds off of tkinter.
root = Tk()

# Tkinter create a canvas to draw on
# This canvas can be seen.
cv = Canvas(root, width=width, height=height, bg='white')
cv.pack()

# PIL create an empty image and draw object to draw on
# This canvas can NOT be seen. It is in the memory only.
image1 = Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image1)


# do the Tkinter canvas drawings (visible)
cv.create_line([0, center, width, center], fill='green', width = 0)
"""the line on the canvas is GREEN, the line saved is RED
This is to show that the canvas is NOT what gets saved. The PIL image is."""

# do the PIL image/draw (in memory) drawings
draw.line([0, center, width, center], red)

cv.bind("<B1-Motion>", paint)

# x4 buttons. Save the image to a file. Clear the canvas. Load the image from the file. Show the hidden canvas
button=Button(text="save", command=save)
button.pack()
button=Button(text="clear", command=clear)
button.pack()
button=Button(text="load", command=load) # not working.
button.pack()
button=Button(text="show", command=show)
button.pack()

root.mainloop()

shanerooney
Автор

why is that dots come while drawing, is there anyway to get smooth lines

vijivijay
Автор

It works for me but the saved image is always blank :(

MarkoBrainz