Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.16 KB | None | 0 0
  1. #drumminhands photobooth
  2. #ideja par qr-booth -
  3.  
  4.  
  5. import os
  6. #import glob
  7. import time
  8. from time import sleep
  9. #import traceback
  10. import picamera
  11. import atexit
  12. #import sys
  13. import socket
  14. import pygame
  15. from pygame.locals import QUIT, KEYDOWN, K_ESCAPE
  16. import config   #fails blakus
  17. #from signal import alarm, signal, SIGALRM, SIGKILL
  18. import RPi.GPIO as GPIO
  19. from PIL import Image, ImageOps, ImageFont, ImageDraw
  20. import subprocess
  21. from shutil import copy2
  22. import cups
  23.  
  24. partyname="MadarasPartyStrip"
  25.  
  26.  
  27. ledPin=7
  28. buttonPin=18
  29. ledState=False
  30.  
  31. total_pics=4
  32. capture_delay=1
  33. prep_delay=1
  34.  
  35. test_server='www.google.com'
  36.  
  37. high_res_w=1296
  38. high_res_h=972
  39.  
  40.  
  41.  
  42. transform_x=config.monitor_w
  43. transform_y=config.monitor_h
  44. offset_x=0
  45. offset_y=0
  46.  
  47.  
  48. stripEvenSpace=False
  49.  
  50. caption='Photo Booth Pics'
  51.  
  52. real_path=os.path.dirname(os.path.realpath(__file__))
  53.  
  54.  
  55.  
  56. GPIO.setmode(GPIO.BOARD)
  57. GPIO.setup(ledPin,GPIO.OUT)
  58. GPIO.setup(buttonPin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
  59.  
  60. pygame.init()
  61. pygame.display.set_mode((config.monitor_w,config.monitor_h))
  62. screen=pygame.display.get_surface()
  63. pygame.display.set_caption(caption)
  64. pygame.mouse.set_visible(False)
  65. pygame.display.toggle_fullscreen()
  66.  
  67. def cleanup():
  68.     print 'cleaning up'
  69.     pygame.quit()
  70.     GPIO.cleanup()
  71. atexit.register(cleanup)
  72.  
  73.  
  74. def input(events):
  75.     for event in events:
  76.         if (event.type==QUIT or
  77.             (event.type == pygame.KEYDOWN and event.key==pygame.K_ESCAPE)):
  78.             print 'exiting'
  79.             exit()
  80.    
  81.    
  82. def is_connected():
  83.     try:
  84.         host=socket.gethostbyname(test_server)
  85.         s=socket.create_connection((host,80),2)
  86.         return True
  87.     except:
  88.         pass
  89.         return False
  90.  
  91.  
  92. def set_dimensions(img_w,img_h):
  93.     global transform_y, transform_x, offset_y,offset_x
  94.    
  95.     ratio_h=(config.monitor_w*img_h)/img_w
  96.    
  97.     if (ratio_h<config.monitor_h):
  98.         transform_y=ratio_h
  99.         transform_x=config.monitor_w
  100.         offset_y=(config.monitor_h-ratio_h)/2
  101.         offset_x=0
  102.        
  103.     elif (ratio_h>config.monitor_h):
  104.         transform_x=(config.monitor_h*img_w)/img_h
  105.         transform_y=config.monitor_h
  106.         offset_x=(config.monitor_w-transform_x)/2
  107.         offset_y=0
  108.     else:
  109.         transform_x=config.monitor_w
  110.         transform_y=config.monitor_h
  111.         offset_y=offset_x=0
  112.        
  113.  
  114. def show_image(fullpath):
  115.     print fullpath
  116.     screen.fill((0,0,0))
  117.     img=pygame.image.load(fullpath)
  118.     img=img.convert()
  119.     set_dimensions(img.get_width(),img.get_height())
  120.     img=pygame.transform.scale(img,(transform_x,transform_y))
  121.     screen.blit(img,(offset_x,offset_y))
  122.     pygame.display.flip()
  123.  
  124. def clear_screen():
  125.     screen.fill( (255,255,255) )
  126.     pygame.display.flip()
  127.  
  128. def makeFinalImage():
  129.    
  130.     canvasSize=(1240,1844)
  131.     offset=80
  132.     offset_correction_h=6
  133.     border=6
  134.     color="white"
  135.     img=Image.new("RGB",canvasSize,"black")
  136.     t_width=img.size[0]/2-offset
  137.     t_height=img.size[1]/2-offset
  138.    
  139.    
  140.     imgList=[]
  141.     for i in range(1,5):
  142.         singleImg=Image.open(str(i)+".jpg")
  143.        
  144.         singleImg.thumbnail((t_width,t_height),Image.ANTIALIAS)
  145.  
  146.         singleImg=ImageOps.expand(singleImg,border,fill=color)
  147.         imgList.append(singleImg)
  148.         singleImg.save(str(i)+"_t.jpg")
  149.  
  150.     imgBg=Image.open("bkg.jpg")
  151.     imgBg=imgBg.resize(canvasSize)
  152.     img.paste(imgBg,(0,0))
  153.  
  154.     if stripEvenSpace:
  155.         rowDim=t_height/2
  156.     else:
  157.         rowDim=singleImg.size[1]
  158.    
  159.     draw=ImageDraw.Draw(img)
  160.     draw.line((img.width/2,0, img.width/2,img.height),fill="white")
  161.    
  162.    
  163.    
  164.     for rowIndex, rowImg in enumerate(imgList):
  165.         for col in range(0,2):
  166.  
  167.            
  168.             if col%2==0:
  169.                  offset_h=offset_correction_h
  170.             else:
  171.                 offset_h=-offset_correction_h
  172.  
  173.  
  174.            
  175.             x=col*canvasSize[0]/2+offset/2+offset_h-border+offset_h
  176.             y=rowIndex*rowDim+offset*(rowIndex+1)/2
  177.            
  178.             print col,col%2, x, offset_h
  179.             img.paste(rowImg,(x,y))
  180.        
  181.  
  182.     now=time.strftime("%Y-%m-%d-%H-%M-%S")
  183.  
  184.  
  185.     img.save("image.jpg","JPEG")
  186.     img.save("/home/pi/anotherPhotoBooth/pics/"+partyName+"_"+now+".jpg")
  187.    
  188. def copy4Images():
  189.     now=time.strftime("%Y-%m-%d-%H-%M-%S")
  190.     for i in range(1,5):
  191.         try:
  192.             ko=str(i)+".jpg"
  193.             kur='/home/pi/anotherPhotoBooth/pics/'+str(i)+"_"+now+time.strftime("_%Y-%m-%d-%H-%M-%S")+".jpg"
  194.             copy2(ko,kur)
  195.         except:
  196.             pass
  197.    
  198.  
  199. def startPhotobooth():
  200.  
  201.     input(pygame.event.get()) #looking for escape
  202.     print 'started'
  203.     print "get ready"
  204.     GPIO.output(ledPin,False)
  205.    
  206.    
  207.    
  208.     sleep(prep_delay)
  209.    
  210.     #clear_screen()
  211.    
  212.     camera=picamera.PiCamera()
  213.     camera.vflip=False
  214.     camera.hflip=True
  215.    
  216.     now=time.strftime("%Y-%m-%d-%H-%M-%S")
  217.    
  218.     if config.hi_res_pics:
  219.         camera.resolution=(high_res_w,high_res_h)
  220.     else:
  221.         pixel_width=500
  222.         pixel_height=config.monitor_h*pixel_width // config.monitor_w
  223.         camera.resolution=(pixel_width,pixel_height)
  224.    
  225.                
  226.     time.sleep(1)
  227.        
  228.     if config.capture_count_pics:
  229.         try:
  230.            
  231.             for i in range(1,total_pics+1):
  232.                 camera.start_preview()
  233.                 #filename=config.file_path+now+'_0'+str(i)+'.jpg'
  234.                
  235.                
  236.                
  237.                 for j in range (5, 0, -1):
  238.                     camera.annotate_text=''+ str(i) +". foto - " +str(j)
  239.                     sleep(0.5)
  240.                     print i
  241.                
  242.                 #camera.capture(filename)
  243.                
  244.                
  245.                
  246.                 camera.stop_preview()
  247.                 show_image("saycheese.jpg")
  248.  
  249.                 subprocess.check_output('gphoto2 --capture-image-and-download --filename "/home/pi/anotherPhotoBooth/'+str(i)+'.jpg" --force-overwrite ', shell=True)
  250.                 #camera.start_preview()
  251.                 #todo check if image exists and then continue
  252.                
  253.                 #jasagatavo gala bilde uzreiz sheit no i-tas bildes un no failneima
  254.          
  255.                
  256.                 time.sleep(capture_delay)   #vajag, lai nesutitu komandu bildet nakamo bildi atrak neka var.
  257.                
  258.                
  259.                 if i==total_pics+1:
  260.                     break
  261.                
  262.             makeFinalImage()
  263.             printFile()
  264.                
  265.         finally:
  266.             camera.close()
  267.  
  268.  
  269.        
  270.        
  271.     show_image(real_path+"/logo.jpg")
  272.  
  273.  
  274.  
  275. def printFile():
  276.     conn=cups.Connection()
  277.     printers=conn.getPrinters()
  278.     printer_name=printers.keys()[0]
  279.    
  280.     print printer_name, 2
  281.     cups.setUser('pi')
  282.    
  283.     print_id=conn.printFile(printer_name,"image.jpg","PhotoBooth",{})
  284.    
  285.     from time import sleep
  286.     while conn.getJobs().get(print_id,None):
  287.         sleep(1)
  288.    
  289.    
  290. if __name__!='__main__':
  291.    
  292.     show_image(real_path+"/logo.jpg")
  293.    
  294.     while True:
  295.         print "led on"
  296.         GPIO.output(ledPin, True)
  297.         input(pygame.event.get())
  298.         print "wait for input"
  299.         GPIO.wait_for_edge(buttonPin, GPIO.FALLING)
  300.         time.sleep(config.debounce)
  301.         startPhotobooth()
  302.  
  303.  
  304. copy4Images()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement