Skip to content

Commit a2669ec

Browse files
authored
Merge pull request #32 from FoamyGuy/custom_filenames
allow custom filenaming
2 parents 3e56953 + 85d7699 commit a2669ec

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

adafruit_pycamera/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,14 @@ def live_preview_mode(self):
817817
# self.effect = self._effect
818818
self.continuous_capture_start()
819819

820-
def open_next_image(self, extension="jpg"):
820+
def open_next_image(self, extension="jpg", filename_prefix="img"):
821821
"""Return an opened numbered file on the sdcard, such as "img01234.jpg"."""
822822
try:
823823
os.stat("/sd")
824824
except OSError as exc: # no SD card!
825825
raise RuntimeError("No SD card mounted") from exc
826826
while True:
827-
filename = "/sd/img%04d.%s" % (self._image_counter, extension)
827+
filename = f"/sd/{filename_prefix}{self._image_counter:04d}.{extension}"
828828
self._image_counter += 1
829829
try:
830830
os.stat(filename)
@@ -834,7 +834,7 @@ def open_next_image(self, extension="jpg"):
834834
print("Writing to", filename)
835835
return open(filename, "wb")
836836

837-
def capture_jpeg(self):
837+
def capture_jpeg(self, filename_prefix="img"):
838838
"""Capture a jpeg file and save it to the SD card"""
839839
try:
840840
os.stat("/sd")
@@ -852,7 +852,7 @@ def capture_jpeg(self):
852852
print(f"Captured {len(jpeg)} bytes of jpeg data")
853853
print("Resolution %d x %d" % (self.camera.width, self.camera.height))
854854

855-
with self.open_next_image() as dest:
855+
with self.open_next_image(filename_prefix=filename_prefix) as dest:
856856
chunksize = 16384
857857
for offset in range(0, len(jpeg), chunksize):
858858
dest.write(jpeg[offset : offset + chunksize])

examples/timestamp_filename/code.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
""" simple point-and-shoot camera example. With NTP and internal RTC to
5+
add timestamp to photo filenames. Must install adafruit_ntp library!
6+
Example code assumes WIFI credentials are properly setup and web workflow
7+
enabled in settings.toml. If not, you'll need to add code to manually connect
8+
to your network."""
9+
10+
import time
11+
import wifi
12+
import socketpool
13+
import rtc
14+
import adafruit_ntp
15+
import adafruit_pycamera # pylint: disable=import-error
16+
17+
pool = socketpool.SocketPool(wifi.radio)
18+
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
19+
rtc.RTC().datetime = ntp.datetime
20+
21+
pycam = adafruit_pycamera.PyCamera()
22+
pycam.mode = 0 # only mode 0 (JPEG) will work in this example
23+
24+
# User settings - try changing these:
25+
pycam.resolution = 2 # 0-12 preset resolutions:
26+
# 0: 240x240, 1: 320x240, 2: 640x480, 3: 800x600, 4: 1024x768,
27+
# 5: 1280x720, 6: 1280x1024, 7: 1600x1200, 8: 1920x1080, 9: 2048x1536,
28+
# 10: 2560x1440, 11: 2560x1600, 12: 2560x1920
29+
pycam.led_level = 1 # 0-4 preset brightness levels
30+
pycam.led_color = 0 # 0-7 preset colors: 0: white, 1: green, 2: yellow, 3: red,
31+
# 4: pink, 5: blue, 6: teal, 7: rainbow
32+
pycam.effect = 0 # 0-7 preset FX: 0: normal, 1: invert, 2: b&w, 3: red,
33+
# 4: green, 5: blue, 6: sepia, 7: solarize
34+
35+
print("Simple camera ready.")
36+
pycam.tone(800, 0.1)
37+
pycam.tone(1200, 0.05)
38+
39+
while True:
40+
pycam.blit(pycam.continuous_capture())
41+
pycam.keys_debounce()
42+
43+
if pycam.shutter.short_count:
44+
print("Shutter released")
45+
pycam.tone(1200, 0.05)
46+
pycam.tone(1600, 0.05)
47+
try:
48+
pycam.display_message("snap", color=0x00DD00)
49+
timestamp = "img_{}-{}-{}_{:02}-{:02}-{:02}_".format(
50+
time.localtime().tm_year,
51+
time.localtime().tm_mon,
52+
time.localtime().tm_mday,
53+
time.localtime().tm_hour,
54+
time.localtime().tm_min,
55+
time.localtime().tm_sec,
56+
)
57+
pycam.capture_jpeg(filename_prefix=timestamp)
58+
pycam.live_preview_mode()
59+
except TypeError as exception:
60+
pycam.display_message("Failed", color=0xFF0000)
61+
time.sleep(0.5)
62+
pycam.live_preview_mode()
63+
except RuntimeError as exception:
64+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
65+
time.sleep(0.5)
66+
67+
if pycam.card_detect.fell:
68+
print("SD card removed")
69+
pycam.unmount_sd_card()
70+
pycam.display.refresh()
71+
72+
if pycam.card_detect.rose:
73+
print("SD card inserted")
74+
pycam.display_message("Mounting\nSD Card", color=0xFFFFFF)
75+
for _ in range(3):
76+
try:
77+
print("Mounting card")
78+
pycam.mount_sd_card()
79+
print("Success!")
80+
break
81+
except OSError as exception:
82+
print("Retrying!", exception)
83+
time.sleep(0.5)
84+
else:
85+
pycam.display_message("SD Card\nFailed!", color=0xFF0000)
86+
time.sleep(0.5)
87+
pycam.display.refresh()

0 commit comments

Comments
 (0)