Photo booth – Raspberry Pi

Photo Booth using the Raspberry Pi and Pi Camera. Designed for 7″ touch screen. Just run  python code in Raspbian.

from __future__ import print_function
import pygame
import time
from picamera import PiCamera
import sys
from PIL import Image

import os

pygame.init()

win = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
win.fill((255,255,255))
camera = PiCamera()
camera.resolution = (800,800)
#camera.color_effects = (128,128) #turn camera to black and white

class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self,win,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline,(self.x-2,self.y-2,self.width+4,self.height+4),0)

        pygame.draw.rect(win, self.color,(self.x,self.y,self.width,self.height),0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0,0,0))
            win.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self, pos):
        #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0]  self.y and pos[1] < self.y + self.height:
                return True

        return False

def redrawWindow():
    win.fill((255,255,255))
    greenButton.draw(win)

def counterWindow():
    win.fill((255,255,255))
    if(counter!=0):
        counterLabel.text = str(counter)
    else:
        counterLabel.text = "Smile"
    counterLabel.draw(win)

def loadimage():
    global imagename
    win.fill((0,0,0))
    img = pygame.image.load(imagename)
    img = pygame.transform.scale(img,(480,480))
    win.blit(img,(170,0))

def takePhoto():
    global showImage
    global counterActive
    global counter
    global imagename
    print("Take Photo")
    for x in range(4):
        camera.start_preview()
        time.sleep(1)
        camera.capture('image'+str(x)+'.jpg')
        camera.stop_preview()

    files = [
  'image0.jpg',
  'image1.jpg',
  'image2.jpg',
  'image3.jpg']

    result = Image.new("RGB", (800, 800))

    for index, file in enumerate(files):
      path = os.path.expanduser(file)
      img = Image.open(path)
      img.thumbnail((400, 400), Image.ANTIALIAS)
      x = index // 2 * 400
      y = index % 2 * 400
      w, h = img.size
      print('pos {0},{1} size {2},{3}'.format(x, y, w, h))
      result.paste(img, (x, y, x + w, y + h))

    imagename = str(round(time.time() * 1000)) +'.jpg'
    result.save(os.path.expanduser(imagename))
    showImage = True
    counterActive = False
    counter = 6

def resetStatus():
    global counter
    global startProcess
    global counterActive
    global imageCounter

    counter = 6
    startProcess = True
    counterActive = False
    imageCounter = 0

run = True
counter = 6
startProcess = True
counterActive = False
showImage = False
greenButton = button((0,255,0),280,200,250,100,'Take Photos')
counterLabel = button((100,255,255),300,200,250,100,'0')
imageCounter = 0
imagename =""

while run:
    print(counter)
    if(startProcess == True):
        redrawWindow()
    elif(counterActive == True):
        counter = counter-1
        counterWindow()
        time.sleep(1)
    elif(showImage == True):
        loadimage()
        time.sleep(1)
        imageCounter = imageCounter +1
        if(imageCounter == 15):
             resetStatus()

    pygame.display.update()

    if(counterActive == False):
        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()

            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if greenButton.isOver(pos):
                    print("Clicked the button")
                    counterActive = True
                    startProcess = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    run = False
                    pygame.quit()
                    quit()

    if(counter == 0):
        takePhoto()

One thought on “Photo booth – Raspberry Pi

Leave a comment