Skip to content

Detect max quality level dynamically. #3018

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 4 commits into from
Apr 14, 2025
Merged
Changes from all 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
23 changes: 21 additions & 2 deletions PiCowbell_Camera_Demos/JPEG_Capture/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,27 @@ def open_next_image():
return open(filename, "wb")

cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG
cam.quality = 5
b = bytearray(cam.capture_buffer_size)

# Different platforms have different amounts of memory available.
# Typically a Pico 2 can handle quality = 2 and a Pico can handle quality = 5.
# Rather than detect and select sizes, let's try to detect the best dynamically
# for broader platform support.
# Start with the highest quality setting and attempt to allocate a buffer
# of the necessary size. If it fails, try the next lowest.
b = None
for quality in range(2,55): #valid range is 2 to 54 inclusive
try:
cam.quality = quality
print(f"Attempting to use quality {quality}.")
b = bytearray(cam.capture_buffer_size)
print(f"Quality {quality} successfully selected.")
break
except MemoryError:
print(f"Quality {quality} was too big. Trying next lowest.")

if b is None:
print("There wasn't enough system memory to allocate the lowest quality buffer.")

jpeg = cam.capture(b)

while True:
Expand Down