-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
31 lines (24 loc) · 926 Bytes
/
camera.py
File metadata and controls
31 lines (24 loc) · 926 Bytes
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
import os
ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
if not os.path.exists('photos'):
os.makedirs('photos')
def get_next_photo_index():
existing_files = os.listdir('photos')
max_index = 0
for file in existing_files:
if file.startswith('captured_photo') and file.endswith('.jpg'):
try:
index = int(file[len('captured_photo'):len(file) - len('.jpg')])
max_index = max(max_index, index)
except ValueError:
continue
return max_index + 1
def save_photo(photo):
if photo and allowed_file(photo.filename):
photo_index = get_next_photo_index()
filename = os.path.join('photos', f"captured_photo{photo_index}.jpg")
photo.save(filename)
return filename
return None