Code : Tout sélectionner
import cups
from tkinter import *
from PIL import Image, ImageTk
from picamera import PiCamera
from datetime import datetime
def clearWindow():
for widget in self.winfo_children():
widget.destroy()
#Menu
def menu():
global noprint
noprint = 2
clearWindow()
img1 = Image.open('barmenu.png')
img2 = Image.open('launch.png')
img3 = Image.open('setting.png')
camera = ImageTk.PhotoImage(img1)
launch = ImageTk.PhotoImage(img2)
setting = ImageTk.PhotoImage(img3)
photoButton = Button(self, image=camera, width=width, height=66, bg='white')
photoButton.image = camera
photoButton.pack(side=TOP)
photoButton = Button(self, image=launch, width=245, height=257, bg='white', command=waitUser)
photoButton.image = launch
photoButton.pack(side=RIGHT)
photoButton = Button(self, image=setting, width=247, height=257, bg='white', command=confg)
photoButton.image = setting
photoButton.pack(side=LEFT)
self.bind('<Key-Escape>', exitProgram)
self.bind('<Escape>', exitProgram)
#APPLIQUATION
def waitUser():
global noprint
noprint = 2
clearWindow()
img = Image.open('backgrounda.png')
camera = ImageTk.PhotoImage(img)
photoButton = Button(self, image=camera, width=width, height=height, bg='blue', activebackground='blue', command=takePhoto)
photoButton.image = camera
photoButton.pack(side=TOP)
#CONFIGURATION APPAREIL
def confg():
global noprint, scaleISO,scaleContrast, scaleBrightness, scaleSaturation # sinon ces objets ne sont accessible que dans cette fonction
noprint = 2
root = Tk()
root.wm_title("CONFIG")
root.geometry('480x320')
bouton1 = Button(root, text="OK")
bouton1.pack(side=BOTTOM)
bouton2 = Button(root, text="TEST", command=takePhoto)
bouton2.pack(side=BOTTOM)
scaleISO = Scale(root, from_=0, to=500, relief='solid', label="ISO", resolution=50, tickinterval=50, length=180)
scaleISO.pack(side=LEFT)
scaleISO.set(100)
scaleContrast = Scale(root, from_=0, to=100, relief='solid', label="CONTS", resolution=10, tickinterval=50, length=180, command=takePhoto)
scaleContrast.pack(side=LEFT)
scaleContrast.set(50)
scaleBrightness = Scale(root, from_=0, to=100, relief='solid', label="LUMI", resolution=10, tickinterval=50, length=180)
scaleBrightness.pack(side=LEFT)
scaleBrightness.set(100)
scaleSaturation = Scale(root, from_=0, to=100, relief='solid', label="SATUR", resolution=10, tickinterval=50, length=180)
scaleSaturation.pack(side=LEFT)
scaleSaturation.set(50)
#COMPTE A REBOURS
def countDown(counter):
clearWindow()
if counter == 0:
self.after(100, capturePicture)
countdown = Label(self, text="Souriez!", font=font, width=width, height=height, bg='white', activebackground='white').pack()
else:
ct = str(counter)
countdown = Label(self, text=counter, font=font, width=width, height=height, bg='white', activebackground='white').pack()
self.after(1000, countDown, counter-1)
#APRECUS PHOTO
def reviewPhoto(filename, counter):
global noprint
if noprint == 1:
waitUser()
return
if noprint == 0:
printCups(filename)
return
if counter == 0:
waitUser()
return
elif counter == 60:
clearWindow()
canvas = Canvas(self, width=width, height=height, bg='black')
canvas.pack()
photo = Image.open(filename)
photo.thumbnail(maxsize, Image.ANTIALIAS)
canvas.photo = ImageTk.PhotoImage(photo) #BOUTON RETOUR
canvas.create_image(53, 0, image=canvas.photo, anchor=NW)
img2 = Image.open('retour.png')
noprint = ImageTk.PhotoImage(img2)
decidePrintNo = Button(self, image=noprint, bg='white', activebackground='white', command=dontPrint)
decidePrintNo.image = noprint
decidePrintNo_window = canvas.create_window(0, 0, anchor=NW, window=decidePrintNo)
if counter > 0:
self.after(1000, reviewPhoto, filename, counter-1)
#BOUTON RETOUR
def doPrint():
global noprint
noprint = 0
def dontPrint():
global noprint
noprint = 1
def printCups(filename):
clearWindow()
img = Image.open('printing.png')
printing = ImageTk.PhotoImage(img)
printingButton = Button(self, image=printing, width=width, height=height, bg='yellow', activebackground='yellow', command=takePhoto)
printingButton.image = printing
printingButton.pack(side=TOP)
conn = cups.Connection ()
printers = conn.getPrinters ()
printer = list(printers)[0]
printqueue = len(conn.getJobs())
if printqueue > 0:
conn.enablePrinter(printer)
clearWindow()
img2 = Image.open('paper.png')
paper = ImageTk.PhotoImage(img2)
paperButton = Button(self, text="!", font=font, image=paper, compound=CENTER, width=int(width/2), height=height, bg='blue', activebackground='blue', command= lambda: printCups(filename))
paperButton.image = paper
paperButton.pack(side=LEFT)
img3 = Image.open('noprint.png')
noprint = ImageTk.PhotoImage(img3)
noprintButton = Button(self, image=noprint, width=int(width/2), height=height, bg='blue', activebackground='blue', command=waitUser)
noprintButton.image = noprint
noprintButton.pack(side=LEFT)
else:
conn.enablePrinter(printer)
conn.printFile(printer, filename, filename, {})
self.after(2000, waitUser)
#PARAMETTRES APPAREIL PHOTO
def takePhoto():
global scaleISO,scaleContrast, scaleBrightness, scaleSaturation # et ici pour pouvoir savoir ou ces objets sont situés
clearWindow()
camera.vflip = True
camera.ISO = scaleISO.get() #ISO
camera.contrast = scaleContrast.get() #CONTRAST
camera.brightness = scaleBrightness.get() #LUMINAUSITE
camera.satration = scaleSaturation.get() #SATURATION
#PARAMETRES PAR DEFAUTS
camera.exposure_mode = 'auto' #MODE D'EXPOSITION
camera.awb_mode = 'auto' # BALANCE DES BLANC
camera.image_effect = 'none' # STYLE DES PHOTOS
camera.ISO = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
#DELAIS COMPTE A REBOURS
countDown(3)
#LIEU D'ENREGISTREMENT
def capturePicture():
filename='/home/pi/Pictures/img_' + datetime.now().strftime(dateString) + '.jpg'
camera.capture(filename)
camera.stop_preview()
reviewPhoto(filename, 60)
def exitProgram(event=None):
self.destroy()
#PARAMETRES DIVERS
self = Tk()
self.attributes("-fullscreen", True)
self.title("Photobooth")
width = self.winfo_screenwidth()
height = self.winfo_screenheight()
maxsize = (width, height)
noprint = 2 # 2=Undecided, 1=Don't print, 0=Print
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
font = ['Billabong', 120]
dateString = '%Y%m%d_%H%M%S'
waitUser()
menu()
self.mainloop()