-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoBooth.py
More file actions
153 lines (125 loc) · 4.61 KB
/
PhotoBooth.py
File metadata and controls
153 lines (125 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/user/bin/env python3
#use "sudo ln -sf /usr/bin/python3.4 /usr/bin/python"
#changing the default python
#To run in console use "python3 PhotoBooth.py"
import tweepy
from subprocess import call
from datetime import datetime
import picamera
import time
from tkinter import *
from tkinter import ttk
import RPi.GPIO as GPIO
from PIL import Image, ImageTk
import urllib
def my_callback(channel):
global pressed
pressed = True
#Summary: The method creates a window that previews the picture taken
#with the ability to input a caption.
def showPreview():
#Using tkinter for creating windows.
#root is created in the main program loop
root.title("Add a caption")
mainframe = ttk.Frame(root, padding="3 3 12 12")
root.update_idletasks()
#To prevent root window from appearing in random locations
#Grabbing the width and height of the current screen and finding
#rough middle of the screen location.
w=mainframe.winfo_screenwidth()
h=mainframe.winfo_screenheight()
size = tuple(int(_) for _ in root.geometry().split('+')[0].split('x'))
x = w/2 - size[0]/2
y = h/2 - size[1]/2
root.geometry("%dx%d+%d+%d" % (800, 480, 0, 0))
#Creating the frame inside the root window.
mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
#Image is handled by PIL
#Open taken from the main loop.
img = Image.open(filePath)
img.thumbnail((500,300), Image.ANTIALIAS)#Define the size of the preview image.
newimg = ImageTk.PhotoImage(img)
ttk.Label(mainframe, image = newimg).grid(column = 0, row = 1,sticky=W)
#Input text box for adding a caption to the image.
captionBox = ttk.Entry(mainframe, width=40, textvariable=caption)
captionBox.grid(column=0, row=3, sticky=(W,E))
#Create tweet button and text box.
ttk.Label(mainframe, text="Add a caption for the tweet!").grid(column=0, row=2, sticky=W)
ttk.Button(root, text="Tweet", command = tweet).grid(column=3, row=3, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
captionBox.focus()
#Bind the enter key to allow same effect as clicking the button.
root.bind('<Return>', tweet)
root.bind('<Escape>', onEscape)
root.protocol("WM_DELETE_WINDOW", onClose)
root.mainloop()
#Method for proper cleanup.
def onClose():
root.destroy()
GPIO.cleanup()
global exit1
exit1 = True
#Escape and reloop
def onEscape(*args):
root.destroy()
#Check internet connection
def check_internet():
try:
urllib.request.urlopen('http://google.com')
return True
except:
return False
def tweet(*args):
if not check_internet():
messagebox.showerror('No internet', 'Check your internet connection.')
return
#Grab tokens from text file
tokenarray = {}
with open("AuthTokens.txt", "r") as file:
for line in file:
key, value = line.split()
tokenarray[key] = value
i = datetime.now() #take time and date for filename
now = i.strftime('%Y%m%d-%H%M%S')
# Consumer keys and access tokens, used for OAuth
consumer_key = tokenarray["consumer_key"]
consumer_secret = tokenarray["consumer_secret"]
access_token = tokenarray["access_token"]
access_token_secret = tokenarray["access_token_secret"]
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# Send the tweet with photo
api.update_with_media(filePath, status=caption.get())
root.destroy()
print(caption.get())
#------
#Main
#------
#Bind the GPIO
GPIO.setmode(GPIO.BCM)
button1_pin = 21
GPIO.setup(button1_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
exit1 = False
#Main loops to allow for continuous image taking.
while not exit1:
pressed = False
fileName = 'pic2.jpg'
filePath = "/home/pi/Desktop/PhotoBooth/" + fileName
root = Tk()#Create root
caption = StringVar()#Define caption text to use in the root window.
#Start camera preview and wait for button press.
with picamera.PiCamera() as camera:
#camera.resoution = (2592, 1944)
camera.start_preview()
try:
GPIO.wait_for_edge(button1_pin, GPIO.FALLING, bouncetime = 500)
except KeyboardInterupt:
onClose()
camera.stop_preview();
camera.capture(fileName)
showPreview()