Commit 771b59e4 authored by Camille Herlent's avatar Camille Herlent
Browse files

Success

parent dfcb7116
#https://www.youtube.com/watch?v=uW-NLL9dlBs
import tkinter
from tkinter import *
import serial
from tkinter.colorchooser import askcolor
from tkinter import filedialog
from PIL import Image
from PIL import ImageGrab
ser = serial.Serial("COM5",baudrate=115200, timeout=0.02)
......@@ -20,13 +20,13 @@ class Paint():
def __init__(self,root):
self.root = root
self.root.geometry("960x544")
self.root.geometry("1080x612")
self.root.configure(background = "white")
self.root.resizable(0,0)
self.root.title("Paint - made in Python")
#Définition fenêtre
self.c = Canvas(self.root, bg='white', width=960, height=544)
self.c = Canvas(self.root, bg='white', width=1080, height=612)
self.c.grid(row=1, columnspan=5)
#Choix outils
......@@ -49,24 +49,23 @@ class Paint():
self.save_button = Button(self.root, text = 'Save',
command = self.save_image)
self.save_button.grid( row = 0, column = 4)
self.c.bind('<B1-Motion>', self.cadre)
#Paramètres initiaux
self.x = 0
self.y = 0
self.dir_x = 0
self.dir_y = 0
self.xrect = 0
self.yrect = 0
self.old_xrect = 0
self.old_yrect = 0
self.zoom = 1
self.old_zoom = 1
self.line_width = 1
self.color = self.DEFAULT_COLOR
self.eraser_on = False
self.active_button = self.pen_button
self.picture = Image.new('RGB', (960, 544), 'white')
#self.draw = ImageDraw.Draw(self.picture)
self.root.after(10, self.paint)
def use_pen(self):
......@@ -86,55 +85,57 @@ class Paint():
some_button.config(relief=SUNKEN)
self.active_button = some_button
self.eraser_on = eraser_mode
def save_image(self):
try :
self.draw = ImageDraw.Draw(self.picture)
filename = filedialog.asksaveasfilename(defaultextension = '.jpg')
#x = self.root.winfo_rootx = self.c.winfo_x()
#y = self.root.winfo_rooty = self.c.winfo_y()
#x1 = x + self.c.winfo_width()
#y1 = y + self.c.winfo_height()
#ImageGrab.grab().crop((x,y,x1,y1))
self.picture.save(filename)
messagebox.showinfo('Success','Image successfully saved as ' + str(filename))
except :
messagebox.showerror('Error','Unable to save')
def paint(self):
recu = ser.read(21) # read 12 bytes
recu = ser.read(15) # read 12 bytes
if ((recu!=b'') and (recu[0]==40) and (len(recu)==21)) :
if ((recu!=b'') and (recu[0]==40) and (len(recu)==15)) :
print(recu)
decodage = recu.decode('utf-8')
self.x = int(decodage[1:4])
self.y = int(decodage[5:8])
#self.zoom = int(decodage[9:11])
self.line_width = int(decodage[9:11])
#self.dir_x = int(decodage[15:17])
#self.dir_y = int(decodage[18:20])
#self.xrect += self.dir_x
#self.yrect += self.dir_y
#self.c.create_rectangle(self.xrect,self.yrect,
#self.xrect + 480*(3-self.zoom),
# self.yrect + 272*(3-self.zoom),
#width = 2, outline = 'red')
x1 = self.x - self.line_width + self.xrect
y1 = self.y - self.line_width + self.yrect
self.zoom = int(decodage[12:14])
if(self.old_zoom == self.zoom):
x1 = (self.x - self.line_width ) * (10-self.zoom)/4 + self.old_xrect
y1 = (self.y - self.line_width ) * (10-self.zoom)/4 + self.old_yrect
x2 = self.x + self.line_width + self.xrect
y2 = self.y + self.line_width + self.yrect
x2 = (self.x + self.line_width ) * (10-self.zoom)/4 + self.old_xrect
y2 = (self.y + self.line_width ) * (10-self.zoom)/4 + self.old_yrect
paint_color = 'white' if self.eraser_on else self.color
self.c.create_oval(x1, y1, x2, y2, fill=paint_color,
paint_color = 'white' if self.eraser_on else self.color
self.c.create_oval(x1, y1, x2, y2, fill=paint_color,
outline = paint_color)
self.root.after(10, self.paint)
def cadre(self,event):
if (self.old_xrect!=event.x) or (self.old_yrect !=event.y) or (self.old_zoom != self.zoom):
self.c.create_rectangle(self.old_xrect , self.old_yrect,
self.old_xrect + 120 * (10-self.old_zoom) + 2 ,
self.old_yrect + 68 * (10-self.old_zoom) + 2,
width = 2, outline = 'white')
self.c.create_rectangle(event.x , event.y,
event.x + 120 * (10-self.zoom)+2,
event.y + 68 * (10-self.zoom)+2,
width = 2, outline = 'red')
self.old_xrect = event.x
self.old_yrect = event.y
self.old_zoom = self.zoom
def save_image(self):
try :
filename = filedialog.asksaveasfilename(defaultextension = '.jpg')
x = Canvas.winfo_rootx(self.c)
y = Canvas.winfo_rooty(self.c)
img = ImageGrab.grab((x,y,x + 1280,y+720)).save(filename)
messagebox.showinfo('Success','Image successfully saved as ' + str(filename))
except :
messagebox.showerror('Error','Unable to save')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment