Skip to content

allow custom filenaming #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions adafruit_pycamera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,17 @@ def live_preview_mode(self):
# self.effect = self._effect
self.continuous_capture_start()

def open_next_image(self, extension="jpg"):
def open_next_image(self, extension="jpg", filename_prefix="img"):
"""Return an opened numbered file on the sdcard, such as "img01234.jpg"."""
try:
os.stat("/sd")
except OSError as exc: # no SD card!
raise RuntimeError("No SD card mounted") from exc
while True:
filename = "/sd/img%04d.%s" % (self._image_counter, extension)
filename = f"/sd/{filename_prefix}%04d.%s" % (
self._image_counter,
extension,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like you to revise this so that it's just one formatting operation. It'd be something like f"/sd/{filename_prefix}{image_counter:04d}.{extension}", I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest commits change that to use a single format operation. It turns out my IDEs code formatter also unintentionally broke the example by splitting a long line string across multiples so I fixed that with .format()

self._image_counter += 1
try:
os.stat(filename)
Expand All @@ -837,7 +840,7 @@ def open_next_image(self, extension="jpg"):
print("Writing to", filename)
return open(filename, "wb")

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

with self.open_next_image() as dest:
with self.open_next_image(filename_prefix=filename_prefix) as dest:
chunksize = 16384
for offset in range(0, len(jpeg), chunksize):
dest.write(jpeg[offset : offset + chunksize])
Expand Down
84 changes: 84 additions & 0 deletions examples/timestamp_filename/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
""" simple point-and-shoot camera example. With NTP and internal RTC to
add timestamp to photo filenames. Must install adafruit_ntp library!
Example code assumes WIFI credentials are properly setup and web workflow
enabled in settings.toml. If not, you'll need to add code to manually connect
to your network."""

import time
import wifi
import socketpool
import rtc
import adafruit_ntp
import adafruit_pycamera # pylint: disable=import-error

pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
rtc.RTC().datetime = ntp.datetime

pycam = adafruit_pycamera.PyCamera()
pycam.mode = 0 # only mode 0 (JPEG) will work in this example

# User settings - try changing these:
pycam.resolution = 2 # 0-12 preset resolutions:
# 0: 240x240, 1: 320x240, 2: 640x480, 3: 800x600, 4: 1024x768,
# 5: 1280x720, 6: 1280x1024, 7: 1600x1200, 8: 1920x1080, 9: 2048x1536,
# 10: 2560x1440, 11: 2560x1600, 12: 2560x1920
pycam.led_level = 1 # 0-4 preset brightness levels
pycam.led_color = 0 # 0-7 preset colors: 0: white, 1: green, 2: yellow, 3: red,
# 4: pink, 5: blue, 6: teal, 7: rainbow
pycam.effect = 0 # 0-7 preset FX: 0: normal, 1: invert, 2: b&w, 3: red,
# 4: green, 5: blue, 6: sepia, 7: solarize

print("Simple camera ready.")
pycam.tone(800, 0.1)
pycam.tone(1200, 0.05)

while True:
pycam.blit(pycam.continuous_capture())
pycam.keys_debounce()

if pycam.shutter.short_count:
print("Shutter released")
pycam.tone(1200, 0.05)
pycam.tone(1600, 0.05)
try:
pycam.display_message("snap", color=0x00DD00)
# pylint: disable=line-too-long
timestamp = (
f"img_{time.localtime().tm_year}-{time.localtime().tm_mon}-{time.localtime().tm_mday}"
f"_{time.localtime().tm_hour:02}-{time.localtime().tm_min:02}-{time.localtime().tm_sec:02}_"
)
pycam.capture_jpeg(filename_prefix=timestamp)
pycam.live_preview_mode()
except TypeError as exception:
pycam.display_message("Failed", color=0xFF0000)
time.sleep(0.5)
pycam.live_preview_mode()
except RuntimeError as exception:
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
time.sleep(0.5)

if pycam.card_detect.fell:
print("SD card removed")
pycam.unmount_sd_card()
pycam.display.refresh()

if pycam.card_detect.rose:
print("SD card inserted")
pycam.display_message("Mounting\nSD Card", color=0xFFFFFF)
for _ in range(3):
try:
print("Mounting card")
pycam.mount_sd_card()
print("Success!")
break
except OSError as exception:
print("Retrying!", exception)
time.sleep(0.5)
else:
pycam.display_message("SD Card\nFailed!", color=0xFF0000)
time.sleep(0.5)
pycam.display.refresh()