Guest User

Untitled

a guest
Dec 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from gpiozero import Button as BTN
  4. from gpiozero import LED
  5. from picamera import PiCamera
  6. from time import gmtime, strftime, sleep
  7. from Tkinter import *
  8. from PIL import Image, ImageTk, ImageEnhance, ImageDraw, ImageFont
  9. from cups import *
  10. from io import BytesIO
  11. import os
  12.  
  13. Exit_Program_btn = BTN(23)
  14. take_picture_btn = BTN(25)
  15. led = LED(8)
  16. output = " "
  17. latest_photo = '/home/pi/allseeingpi/latest.jpg'
  18. Temp_Pic = '/home/pi/allseeingpi/temp_pic.jpg'
  19. montage_file = '/home/pi/allseeingpi/montage.jpg'
  20. photo_banner = '/home/pi/allseeingpi/Photo_Bottom.jpg'
  21. Status = "Press button to start" #21
  22.  
  23. def Exit_Program():
  24. camera.stop_preview()
  25. led.off()
  26. root.destroy()
  27. print("Tkinter window closed")
  28.  
  29. def take_picture():
  30. global output
  31. global Temp_Pic
  32. global latest_photo
  33. global i
  34.  
  35. #get the number from the text file and overwrite it to the next number
  36. num = open('/home/pi/allseeingpi/number.txt','r')
  37. numb = int(num.read())
  38. num.close()
  39. numbe = open('/home/pi/allseeingpi/number.txt','w')
  40. numbe.write(str(numb+1))
  41. numbe.close()
  42.  
  43. #set the output file location and name
  44. output = "/home/pi/allseeingpi/image-" + str(numb) + ".png"
  45.  
  46. #Capture picture and save to Temp_Pic
  47. camera.capture(Temp_Pic)
  48. if i==4:
  49. camera.stop_preview()
  50. led.off()
  51.  
  52. #resize the image for keeps and montage
  53. tn_size = 516, 344
  54. #size = 1800, 1200
  55. jpg_img1 = Image.open(Temp_Pic)#, resize = tn_size)
  56. jpg_img = jpg_img1.crop((0,108,2592,1836))
  57. jpg_img1.close()
  58. #jpg_img.resize(size, Image.BICUBIC)
  59. jpg_tbn = jpg_img.resize(tn_size, Image.BICUBIC)
  60. jpg_tbn.save(latest_photo, 'jpeg')
  61. jpg_img.save(output, 'jpeg')
  62. jpg_img.close()
  63. jpg_tbn.close()
  64.  
  65.  
  66. def montage_pics():
  67. global montage_file
  68. global i
  69. global latest_photo
  70.  
  71. #gap = 42, img_width = 516, img_height = 344
  72. x_offset = 687#42
  73. x_offset1 = 1287#642
  74. y_offset = 42*i+344*(i-1)
  75. y_offset1 = 42*(i+1)+344*i
  76.  
  77. montage = Image.open(montage_file)
  78. latest_pic = Image.open(latest_photo)
  79.  
  80. montage.paste(latest_pic, (x_offset, y_offset))
  81. montage.paste(latest_pic, (x_offset1, y_offset))
  82. if i==4:
  83. pic_bottom = Image.open(photo_banner)
  84. montage.paste(pic_bottom, (x_offset, y_offset1))
  85. montage.paste(pic_bottom, (x_offset1, y_offset1))
  86. enhancer = ImageEnhance.Brightness(montage)
  87. montage = enhancer.enhance(1.4)
  88. montage.save(montage_file)
  89. montage.close()
  90.  
  91. def printPhoto():
  92. conn = Connection()
  93. printers = conn.getPrinters()
  94. printer_name = printers.keys()[0]
  95. conn.printFile(printer_name,montage_file,"TITLE",{})
  96. sleep(80)#should be 90sec
  97. #conn.cancelAllJobs(name = 'Canon_MG6100_series')
  98. os.system('/home/pi/allseeingpi/Restart_Cups.sh')
  99. sleep(5)
  100.  
  101. def picture_sequence():
  102. global i
  103. global Status
  104. global var
  105.  
  106. camera.start_preview()
  107. led.on()
  108.  
  109. for x in range(4):
  110. i = int(x) + 1
  111. if x <= 2:
  112. pip = camera.add_overlay(Image.open('/home/pi/allseeingpi/3g.jpg').tobytes(), size = (128,192), window = (500, 150, 128, 192), format='rgb', layer = 3, alpha=128, fullscreen = False)#size=(840, 560),format='rgb', window = (100, 100, 128,192), fullscreen = False,
  113. Status = " Picture " + str(i) + " of 4" #22
  114. var.set(Status)
  115. sleep(0.5)
  116. pip.update((Image.open('/home/pi/allseeingpi/2g.jpg')).tobytes())
  117. #Status = " Picture " + str(i) + " of 4 in 2s " #22
  118. var.set(Status)
  119. sleep(0.5)
  120. pip.update((Image.open('/home/pi/allseeingpi/1g.jpg')).tobytes())
  121. #Status = " Picture " + str(i) + " of 4 in 1s " #22
  122. var.set(Status)
  123. sleep(0.5)
  124. camera.remove_overlay(pip)
  125. pip.close()
  126. Status = " Processing... "#21
  127. var.set(Status)
  128. take_picture()
  129. montage_pics()
  130. else:
  131. pip = camera.add_overlay(Image.open('/home/pi/allseeingpi/3g.jpg').tobytes(), size = (128,192), window = (500, 150, 128, 192), format='rgb', layer = 3, alpha=128, fullscreen = False)#size=(840, 560),format='rgb', window = (100, 100, 128,192), fullscreen = False,
  132. Status = " Picture " + str(i) + " of 4" #22
  133. var.set(Status)
  134. sleep(0.5)
  135. pip.update((Image.open('/home/pi/allseeingpi/2g.jpg')).tobytes())
  136. #Status = " Picture " + str(i) + " of 4 in 2s " #22
  137. var.set(Status)
  138. sleep(0.5)
  139. pip.update((Image.open('/home/pi/allseeingpi/1g.jpg')).tobytes())
  140. #Status = " Picture " + str(i) + " of 4 in 1s " #22
  141. var.set(Status)
  142. sleep(0.5)
  143. camera.remove_overlay(pip)
  144. pip.close()
  145. Status = " Processing... "#21
  146. var.set(Status)
  147. take_picture()
  148. Status = "Compiling and Printing" #22
  149. var.set(Status)
  150. montage_pics()
  151. printPhoto()
  152. Status = "Press button to start" #21
  153. var.set(Status)
  154.  
  155. Exit_Program_btn.when_pressed = Exit_Program
  156. take_picture_btn.when_pressed = picture_sequence
  157.  
  158. camera = PiCamera()
  159. camera.resolution = (2592, 1944) #Pic taken at full res to allow full preview
  160. camera.hflip = True
  161. camera.awb_mode = 'auto'
  162. camera.preview_fullscreen = False
  163. camera.preview_window = (155, 3, 840, 560)#825, 550)#(210, 150, 600, 400)
  164.  
  165. class Window(Frame):
  166.  
  167. # Define settings upon initialization. Here you can specify
  168. def __init__(self, master=None):
  169.  
  170. # parameters that you want to send through the Frame class.
  171. Frame.__init__(self, master)
  172.  
  173. #reference to the master widget, which is the tk window
  174. self.master = master
  175.  
  176. #with that, we want to then run init_window, which doesn't yet exist
  177. self.init_window()
  178.  
  179. def init_window(self):
  180.  
  181. # changing the title of our master widget
  182. self.master.title("GUI")
  183.  
  184. # allowing the widget to take the full space of the root window
  185. self.pack(fill=BOTH, expand=1)
  186.  
  187. #Load and place background image
  188. load = Image.open("/home/pi/allseeingpi/background.jpg")
  189. render = ImageTk.PhotoImage(load)
  190. img = Label(self, image=render)
  191. img.image = render
  192. img.place(x=0, y=0)
  193. load.close()
  194.  
  195. #Create text variable to update status to user
  196. txt = Label(root, textvariable=var, justify=RIGHT, bg="white", font=("Courier", 32))#, wraplength=398) anchor=S, padx = 1000, justify=CENTER,
  197. txt.place(x=290, y=550)
  198. #txt.pack()
  199.  
  200. var.set(Status)
  201.  
  202. root = Tk()
  203. root.geometry("1024x600")
  204.  
  205. var = StringVar(root, value=" ")
  206. app = Window(root)
  207. root.overrideredirect(True) #no borders
  208. root.config(cursor="none")
  209.  
  210. root.mainloop()
Add Comment
Please, Sign In to add comment