Python Forum
Error (Please help!!!!!)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error (Please help!!!!!)
#1
I and my tutor have been working on this project but it doesn't work. Even he can't find the error please help Wall.
(It is supposed to let you draw and then save your picture)

Many thanks,
foksikrasa
from tkinter import *
from PIL import Image, ImageDraw
import io
import os
import subprocess

canvas_width = 1000
canvas_height = 1000
color = 'black'
brush_size = 1
def paint(event):
    global brush_size
    global color
    x1 = event.x - brush_size
    x2 = event.x + brush_size
    y1 = event.y - brush_size
    y2 = event.y + brush_size
    w.create_oval(x1, y1, x2, y2, fill = color, outline=color)

def save_as_gpg():
#    saveIMG()
    print("Picture saving...")
    w.postscript(file = "Picture.eps", colormode='color')
    img = Image.open("Picture.eps")
#    draw = ImageDraw.Draw(img)
#    draw.text((10, 20), "Chao!", (0, 0, 0))
    img.save("Picture.png", "png")

def brush_size_change(new_brush_size):
    global brush_size
    brush_size += new_brush_size
    if brush_size < 1:
        brush_size = 1
    set_brush_size(brush_size)
        
def color_change(new_color):
    global color
    color = new_color
    
def set_brush_size(new_brush_size):
    brush_label.configure(text = "brush = " + str(new_brush_size))
   
def update_brush_size_from_entry():
    global brush_size
    brush_size = int(b_entry.get())
    if brush_size < 1:
        brush_size = 1
    set_brush_size(brush_size)
    
def saveIMG():
    print("Picture saving...")
    w.update()
    w.postscript(file = 'Picture.eps', colormode = "color")

root = Tk()
root.title('Paint')
w = Canvas(root, width = canvas_width, height = canvas_height, bg='white')
w.bind('<B1-Motion>', paint)
w.grid(row=2, column=5)
w.columnconfigure(6, weight=1)
w.rowconfigure(2, weight=1)

red_btn = Button(text='Красный цвет', width=10, command = lambda: color_change('red'), bg = 'red')
red_btn.grid(row =  0, column=6)

Black_btn = Button(text='чёрный', width=10, command = lambda: color_change('black'), bg = 'black')
Black_btn.grid(row =  0, column=1)

Purple_btn = Button(text='фиолетовый', width=10, command = lambda: color_change('purple'), bg = 'purple')
Purple_btn.grid(row =  0, column=2)

Blue_btn = Button(text='синий', width=10, command = lambda: color_change('blue'), bg = 'blue')
Blue_btn.grid(row =  0, column=3)

green_btn = Button(text='Зеленый', width=10, command = lambda: color_change('green'), bg = 'green')
green_btn.grid(row =  0, column=4)

Orange_btn = Button(text='оранжевый', width=10, command = lambda: color_change('orange'), bg = 'orange')
Orange_btn.grid(row =  0, column=7)

Yellow_btn = Button(text='желтый', width=10, command = lambda: color_change('yellow'), bg = 'yellow')
Yellow_btn.grid(row =  0, column=8)

white_btn = Button(text='Ластик', width=10, command = lambda: color_change('white'), bg = 'white')
white_btn.grid(row =  0, column=9)

clear_btn = Button(text='Стереть все', width=10, command = lambda: w.delete('all'))
clear_btn.grid(row=0,column=5)

brush_plus = Button(text='Bigger', width=10, command = lambda: brush_size_change(1))
brush_plus.grid(row=3,column=1)

brush_minus = Button(text='Smaller', width=10, command = lambda: brush_size_change(-1))
brush_minus.grid(row=3,column=2)

b_entry = Entry(root, width = 10)
b_entry.grid(row=3,column=4)

update_brush_size = Button(text='Update', width=10, command = lambda: update_brush_size_from_entry())
update_brush_size.grid(row=3,column=5)

save_btn = Button(text='Save', width=10, command = lambda: save_as_gpg())
save_btn.grid(row=3,column=6)

brush_label = Label(root, width=10, text = "brush =")
brush_label.grid(row=3,column=3)


root.mainloop()
Reply
#2
Please, post the full traceback you get, in error tags.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ok:
Error:
Picture saving... Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\mishk\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Users\mishk\Desktop\thonny programs\drawing.py", line 102, in <lambda> save_btn = Button(text='Save', width=10, command = lambda: save_as_gpg()) File "C:\Users\mishk\Desktop\thonny programs\drawing.py", line 27, in save_as_gpg img.save("Picture.png", "png") File "C:\Users\mishk\AppData\Local\Programs\Thonny\lib\site-packages\PIL\Image.py", line 2100, in save self._ensure_mutable() File "C:\Users\mishk\AppData\Local\Programs\Thonny\lib\site-packages\PIL\Image.py", line 617, in _ensure_mutable self._copy() File "C:\Users\mishk\AppData\Local\Programs\Thonny\lib\site-packages\PIL\Image.py", line 610, in _copy self.load() File "C:\Users\mishk\AppData\Local\Programs\Thonny\lib\site-packages\PIL\EpsImagePlugin.py", line 332, in load self.im = Ghostscript(self.tile, self.size, self.fp, scale) File "C:\Users\mishk\AppData\Local\Programs\Thonny\lib\site-packages\PIL\EpsImagePlugin.py", line 134, in Ghostscript raise OSError("Unable to locate Ghostscript on paths") OSError: Unable to locate Ghostscript on paths
Reply
#4
Do you have Ghostscript installed and added to PATH?
In order to read EPS file in Pillow it requires Ghostscript
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
what is PATH? my tutor installed ghostscript into system shell (I use thonny)spelt as I have just written it
Reply
#6
he installed it - good, but obviously it's not system wide accessible - it cannot find it
what is PATH
I don't know if using Thonny as IDE can affect it (i.e. does Thonny need some extar setting). Try to execute your script from cmd (i.e. not from within Thonny)
Also in cmd you can check the installation and see if you type Ghostscript --help it will display help
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020