0

I am working on the Paint editor in python. Below is my code of paint editor using tkinter. Now I want to save the image of the drawing in editor as image file. The below code works fine and it save the image as well using the PIL library of Python but the problem is that I am integrating this editor with Fusion360 tool which don't have support of PIL ?

Can anyone tell me the alternative ways to save the image without PIL? Or is there any way to convert the convas of tkinter to image file.

from tkinter import *
from tkinter.filedialog import asksaveasfilename as saveAs
import PIL
from PIL import Image, ImageDraw

def save():
    image1.save("usman.png")

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

Output : enter image description here

Usman
  • 1,983
  • 15
  • 28
  • To be honest, you can take screenshots of the window, but I THINK those screenshot libraries also use PIL to grab the image and save it. – Delrius Euphoria Dec 12 '20 at 17:25
  • There are lots of ways! You could write a bitmap file generator in standard python relatively easily. – JeffUK Dec 12 '20 at 17:26

0 Answers0