Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #drumminhands photobooth
- #ideja par qr-booth -
- import os
- #import glob
- import time
- from time import sleep
- #import traceback
- import picamera
- import atexit
- #import sys
- import socket
- import pygame
- from pygame.locals import QUIT, KEYDOWN, K_ESCAPE
- import config #fails blakus
- #from signal import alarm, signal, SIGALRM, SIGKILL
- import RPi.GPIO as GPIO
- from PIL import Image, ImageOps, ImageFont, ImageDraw
- import subprocess
- from shutil import copy2
- import cups
- partyname="MadarasPartyStrip"
- ledPin=7
- buttonPin=18
- ledState=False
- total_pics=4
- capture_delay=1
- prep_delay=1
- test_server='www.google.com'
- high_res_w=1296
- high_res_h=972
- transform_x=config.monitor_w
- transform_y=config.monitor_h
- offset_x=0
- offset_y=0
- stripEvenSpace=False
- caption='Photo Booth Pics'
- real_path=os.path.dirname(os.path.realpath(__file__))
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(ledPin,GPIO.OUT)
- GPIO.setup(buttonPin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
- pygame.init()
- pygame.display.set_mode((config.monitor_w,config.monitor_h))
- screen=pygame.display.get_surface()
- pygame.display.set_caption(caption)
- pygame.mouse.set_visible(False)
- pygame.display.toggle_fullscreen()
- def cleanup():
- print 'cleaning up'
- pygame.quit()
- GPIO.cleanup()
- atexit.register(cleanup)
- def input(events):
- for event in events:
- if (event.type==QUIT or
- (event.type == pygame.KEYDOWN and event.key==pygame.K_ESCAPE)):
- print 'exiting'
- exit()
- def is_connected():
- try:
- host=socket.gethostbyname(test_server)
- s=socket.create_connection((host,80),2)
- return True
- except:
- pass
- return False
- def set_dimensions(img_w,img_h):
- global transform_y, transform_x, offset_y,offset_x
- ratio_h=(config.monitor_w*img_h)/img_w
- if (ratio_h<config.monitor_h):
- transform_y=ratio_h
- transform_x=config.monitor_w
- offset_y=(config.monitor_h-ratio_h)/2
- offset_x=0
- elif (ratio_h>config.monitor_h):
- transform_x=(config.monitor_h*img_w)/img_h
- transform_y=config.monitor_h
- offset_x=(config.monitor_w-transform_x)/2
- offset_y=0
- else:
- transform_x=config.monitor_w
- transform_y=config.monitor_h
- offset_y=offset_x=0
- def show_image(fullpath):
- print fullpath
- screen.fill((0,0,0))
- img=pygame.image.load(fullpath)
- img=img.convert()
- set_dimensions(img.get_width(),img.get_height())
- img=pygame.transform.scale(img,(transform_x,transform_y))
- screen.blit(img,(offset_x,offset_y))
- pygame.display.flip()
- def clear_screen():
- screen.fill( (255,255,255) )
- pygame.display.flip()
- def makeFinalImage():
- canvasSize=(1240,1844)
- offset=80
- offset_correction_h=6
- border=6
- color="white"
- img=Image.new("RGB",canvasSize,"black")
- t_width=img.size[0]/2-offset
- t_height=img.size[1]/2-offset
- imgList=[]
- for i in range(1,5):
- singleImg=Image.open(str(i)+".jpg")
- singleImg.thumbnail((t_width,t_height),Image.ANTIALIAS)
- singleImg=ImageOps.expand(singleImg,border,fill=color)
- imgList.append(singleImg)
- singleImg.save(str(i)+"_t.jpg")
- imgBg=Image.open("bkg.jpg")
- imgBg=imgBg.resize(canvasSize)
- img.paste(imgBg,(0,0))
- if stripEvenSpace:
- rowDim=t_height/2
- else:
- rowDim=singleImg.size[1]
- draw=ImageDraw.Draw(img)
- draw.line((img.width/2,0, img.width/2,img.height),fill="white")
- for rowIndex, rowImg in enumerate(imgList):
- for col in range(0,2):
- if col%2==0:
- offset_h=offset_correction_h
- else:
- offset_h=-offset_correction_h
- x=col*canvasSize[0]/2+offset/2+offset_h-border+offset_h
- y=rowIndex*rowDim+offset*(rowIndex+1)/2
- print col,col%2, x, offset_h
- img.paste(rowImg,(x,y))
- now=time.strftime("%Y-%m-%d-%H-%M-%S")
- img.save("image.jpg","JPEG")
- img.save("/home/pi/anotherPhotoBooth/pics/"+partyName+"_"+now+".jpg")
- def copy4Images():
- now=time.strftime("%Y-%m-%d-%H-%M-%S")
- for i in range(1,5):
- try:
- ko=str(i)+".jpg"
- kur='/home/pi/anotherPhotoBooth/pics/'+str(i)+"_"+now+time.strftime("_%Y-%m-%d-%H-%M-%S")+".jpg"
- copy2(ko,kur)
- except:
- pass
- def startPhotobooth():
- input(pygame.event.get()) #looking for escape
- print 'started'
- print "get ready"
- GPIO.output(ledPin,False)
- sleep(prep_delay)
- #clear_screen()
- camera=picamera.PiCamera()
- camera.vflip=False
- camera.hflip=True
- now=time.strftime("%Y-%m-%d-%H-%M-%S")
- if config.hi_res_pics:
- camera.resolution=(high_res_w,high_res_h)
- else:
- pixel_width=500
- pixel_height=config.monitor_h*pixel_width // config.monitor_w
- camera.resolution=(pixel_width,pixel_height)
- time.sleep(1)
- if config.capture_count_pics:
- try:
- for i in range(1,total_pics+1):
- camera.start_preview()
- #filename=config.file_path+now+'_0'+str(i)+'.jpg'
- for j in range (5, 0, -1):
- camera.annotate_text=''+ str(i) +". foto - " +str(j)
- sleep(0.5)
- print i
- #camera.capture(filename)
- camera.stop_preview()
- show_image("saycheese.jpg")
- subprocess.check_output('gphoto2 --capture-image-and-download --filename "/home/pi/anotherPhotoBooth/'+str(i)+'.jpg" --force-overwrite ', shell=True)
- #camera.start_preview()
- #todo check if image exists and then continue
- #jasagatavo gala bilde uzreiz sheit no i-tas bildes un no failneima
- time.sleep(capture_delay) #vajag, lai nesutitu komandu bildet nakamo bildi atrak neka var.
- if i==total_pics+1:
- break
- makeFinalImage()
- printFile()
- finally:
- camera.close()
- show_image(real_path+"/logo.jpg")
- def printFile():
- conn=cups.Connection()
- printers=conn.getPrinters()
- printer_name=printers.keys()[0]
- print printer_name, 2
- cups.setUser('pi')
- print_id=conn.printFile(printer_name,"image.jpg","PhotoBooth",{})
- from time import sleep
- while conn.getJobs().get(print_id,None):
- sleep(1)
- if __name__!='__main__':
- show_image(real_path+"/logo.jpg")
- while True:
- print "led on"
- GPIO.output(ledPin, True)
- input(pygame.event.get())
- print "wait for input"
- GPIO.wait_for_edge(buttonPin, GPIO.FALLING)
- time.sleep(config.debounce)
- startPhotobooth()
- copy4Images()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement