# MEMENTO Focus Stacking

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/127/638/medium800/camera_flower2_set3.jpg?1707756167 focus stacked macro)

Just like our eyes, cameras can focus at different distances, but not all distances at once. Hold your hand up in front of your face and look at something far away -- your hand will appear blurry. Now focus your eyes on your hand and the background will blur. Camera lenses and sensors do the same thing.

### Focus Stacking

But we don't have to put up with reality! We can take a series of photos with the MEMENTO, each with a different focal length, and then use some clever software to combine them into a single, in-focus-at-all-distances image. This is especially useful when taking close-up macro photographs, such as the flowers seen here.

In this guide we'll cover focus stacking as well as two other neat tricks -- rack focus and split focus -- you can do with the MEMENTO.

These are the extremes of the far- and near-focus shots taken to stack into the single image seen above. Hover your mouse over the two thumbnails to see the larger image comparison.

Below is the sequence of images used for the full focus stack.

![camera_img0115b.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/705/medium640/camera_img0115b.jpg?1707931039)

![camera_img0134b.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/707/medium640/camera_img0134b.jpg?1707931071)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/676/medium800/camera_focus_frames.jpg?1707849072)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/642/medium800/camera_mementoFlowers.jpg?1707757181)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/683/medium800thumb/camera_figrow3.jpg?1707877389)

## Parts
### MEMENTO - Python Programmable DIY Camera - Bare Board

[MEMENTO - Python Programmable DIY Camera - Bare Board](https://www.adafruit.com/product/5420)
Make memories, or just a cool camera-based project,&nbsp;with **Adafruit's MEMENTO Camera Board**. It's a development board with everything you need to create programmable camera and vision projects: with a camera module, TFT preview screen, buttons, SD card slot and...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5420)
[Related Guides to the Product](https://learn.adafruit.com/products/5420/guides)
![Video of a DIY camera on a lazy susan.](https://cdn-shop.adafruit.com/product-videos/640x480/5420-05.jpg)

### Adafruit MEMENTO Camera Enclosure & Hardware Kit

[Adafruit MEMENTO Camera Enclosure & Hardware Kit](https://www.adafruit.com/product/5843)
Once you've picked up your **MEMENTO Camera** and you're ready to take it out into the world, here is a chic and minimalist enclosure that will look great on the runways of Paris or the street photography of New York City! These front and back plates have been...

In Stock
[Buy Now](https://www.adafruit.com/product/5843)
[Related Guides to the Product](https://learn.adafruit.com/products/5843/guides)
![Overhead shot of two square-shaped PCB boards for a DIY camera above eight black plastic screws and four black plastic hex nuts with 3pin to 3pin JST PH cable and adhesive sticker.](https://cdn-shop.adafruit.com/640x480/5843-05.jpg)

### USB Type A to Type C Cable - approx 1 meter / 3 ft long

[USB Type A to Type C Cable - approx 1 meter / 3 ft long](https://www.adafruit.com/product/4474)
As technology changes and adapts, so does Adafruit. This&nbsp;&nbsp; **USB Type A to Type C** cable will help you with the transition to USB C, even if you're still totin' around a USB Type A hub, computer or laptop.

USB C is the latest industry-standard connector for...

In Stock
[Buy Now](https://www.adafruit.com/product/4474)
[Related Guides to the Product](https://learn.adafruit.com/products/4474/guides)
![Angled shot of a coiled black, USB-C to USB-A cable.](https://cdn-shop.adafruit.com/640x480/4474-02.jpg)

### SD/MicroSD Memory Card (8 GB SDHC)

[SD/MicroSD Memory Card (8 GB SDHC)](https://www.adafruit.com/product/1294)
Add mega-storage in a jiffy using this 8 GB class 4 micro-SD card. It comes with a SD adapter so you can use it with any of our shields or adapters. Preformatted to FAT so it works out of the box with our projects. Tested and works great with our <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1294)
[Related Guides to the Product](https://learn.adafruit.com/products/1294/guides)
![Hand removing/installing micro SD card from SD adapter](https://cdn-shop.adafruit.com/640x480/1294-03.jpg)

Warning: 

# MEMENTO Focus Stacking

## Focus Tricks

You'll create focus-stacked macro photos such as the flowers seen here by shooting a series of images at varied focal distances and combining them with clever software.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/673/medium800/camera_flower2_set1.jpg?1707847495)

Info: 

### Rack Focus

You can also use focal distance to draw attention to different subjects in a scene. In cinematography, a rack focus is a technique used to do exactly this. With the MEMENTO you can take a series of images with incrementally varied focal distance and then combine them into a GIF or video.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/674/medium800thumb/camera_figrow3.jpg?1707848539)

### Split Diopter

Another popular focus trick used in films is the split diopter shot, which brings half of the frame into near focus and half into far focus. This is traditionally achieved with a special half-frame lens element secured in front of the main lens, but you can use a simple matting technique to combine two focused frames into one.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/682/medium800thumb/camera_larslapy.jpg?1707873139)

# MEMENTO Focus Stacking

## Install CircuitPython

[CircuitPython](https://github.com/adafruit/circuitpython) is a derivative of [MicroPython](https://micropython.org) designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the **CIRCUITPY** drive to iterate.

## CircuitPython Quickstart

Follow this step-by-step to quickly get CircuitPython running on your board.

Warning: Make sure you use CircuitPython 9.0.0 final or later. CircuitPython 9.0.0-beta.1 and earlier have a bug that can corrupt the fileystem.

```auto
import storage
storage.erase_filesystem()
```

Your board will reboot after running this.

[Download the latest version of CircuitPython for this board via circuitpython.org](https://circuitpython.org/board/adafruit_esp32s3_camera/)
Warning: As of CircuitPython 9, you'll need to create a folder called "sd" on your CIRCUITPY drive to mount the microSD card, if it's not already there.

[Follow these steps to create the /sd directory](https://learn.adafruit.com/adafruit-memento-camera-board/circuitpython-memento-starter-projects)
 **Click the link above to download the latest CircuitPython UF2 file.**

Save it wherever is convenient for you.

![install_circuitpython_on_most_boards_CircuitPython_downloaded.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/129/medium640/install_circuitpython_on_most_boards_CircuitPython_downloaded.jpg?1620922559)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/734/medium800/adafruit_products_resetNeo.jpg?1702908010)

Plug your board into your computer, using a known-good data-sync cable, directly, or via an adapter if needed.

Double-click the **reset** button (highlighted in red above), and you will see the **RGB status LED(s)** turn green (highlighted in green above). If you see red, try another port, or if you're using an adapter or hub, try without the hub, or different adapter or hub.

For this board, tap reset and wait for the LED to turn purple, and as soon as it turns purple, tap reset again. The second tap needs to happen while the LED is still purple.

If you do not see the LED turning purple, you will need to reinstall the UF2 bootloader. See the **Factory Reset** page in this guide for details.

If double-clicking doesn't work the first time, try again. Sometimes it can take a few tries to get the rhythm right!

A lot of people end up using charge-only USB cables and it is very frustrating! **Make sure you have a USB cable you know is good for data sync.**

You will see a new disk drive appear called **CAMERABOOT**. Drag the **adafruit-circuitpython-adafruit\_esp32s3\_camera-etc.uf2** file to **CAMERABOOT**.

![adafruit_products_cameraboot.png](https://cdn-learn.adafruit.com/assets/assets/000/126/740/medium640/adafruit_products_cameraboot.png?1702932896)

The **BOOT** drive will disappear and a new disk drive called **CIRCUITPY** will appear.

That's it!

![install_circuitpython_on_most_boards_CIRCUITPY.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/130/medium640/install_circuitpython_on_most_boards_CIRCUITPY.jpg?1620923145)

# MEMENTO Focus Stacking

## Code the Focus Stacker

The focus stacking mode has been added to the main "fancy camera" example. You can see [this page](https://learn.adafruit.com/adafruit-memento-camera-board/fancy-camera) on using the features as originally created.

There are two variables to adjust in the code -- `FOCUS_STEPS` and `FOCUS_START`.

The `FOCUS_STEPS` default to `20` which means the lens will adjust 20 steps per photo taken, until the maximum near focus value of 255 has been reached. Set this to 5 or 10 for finer focal steps (and more images saved to your SD card.)

`FOCUS_START` is set to `30` by default, meaning the first photo taken won't be at maximum far focus, which is often much farther than needed for typical medium shots. Set this down to 0 for full far focus.

## Download the Project Bundle

Your project will use a specific set of CircuitPython libraries, and the&nbsp; **code.py** &nbsp;file. To get everything you need, click on the&nbsp; **Download Project Bundle** &nbsp;button below, and uncompress the .zip file.

Connect your computer to the board via a known good USB power+data cable. A new flash drive should show up as&nbsp; **CIRCUITPY**.

Drag the contents of the uncompressed bundle directory onto your board&nbsp; **CIRCUITPY** &nbsp;drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/MEMENTO/Memento_Focus_Stack/code.py

## How the Code Works
The code works the same way as the primary camera example for the `adafruit_pycamera` library, [as covered here](https://learn.adafruit.com/adafruit-memento-camera-board/fancy-camera), with the following additions:

### Variables

The `STACK` variable is set to `True`, which means pressing the shutter release in `JPEG` mode will begin shooting the multiple focus images. Set this `False` to use regular snapshot mode.

`FOCUS_STEPS` specifies how many steps to adjust the lens per shot. The voice coil motor can be set from `0-255` (far-to-near focus length) so a step setting of `10` would generate 25 steps. Settings from `5-20` are typical, although you can go all the way down to `1` if you want to capture with the highest focal granularity.

`FOCUS_START` is used to set the farthest position in case you are shooting a medium or close range shot, you won't need a lot of frames captured at the far distances. `0` is the farthest possible focus, objects 12" away from the camera will be in focus starting around the `95` mark, 4" is in focus at about `130`.

`focus_stacking` is used to hold the state while the set of photos is being shot.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/708/medium800/camera_dist_95.jpg?1707937801 Focus 95)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/709/medium800/camera_dist_130.jpg?1707937803 focus 130)

```auto
STACK = True  # mode placeholder
FOCUS_STEPS = 20  # number of focus steps to increment during bracket from 0-255
FOCUS_START = 30  # optionally, start the focus closer
focus_stacking = False
```

### Focus Stack

With the MEMENTO in JPEG mode and the `STACK` set True, when you press the short shutter release here's what happens:

- `pycam.autofocus_vcm_step` is set to the `FOCUS_START` value -- this moves the lens to its farthest focal distance (physically closer to the sensor) by energizing the voice coil motor a precise amount
- the **exposure** , **gain** , and **white balance** values are set and locked so there is no perceived flickering between frames

```auto
pycam.autofocus_vcm_step = FOCUS_START
saved_settings = pycam.get_camera_autosettings()
pycam.set_camera_exposure(saved_settings["exposure"])
pycam.set_camera_gain(saved_settings["gain"])
pycam.set_camera_wb(saved_settings["wb"])
```

### Focus, Shoot, Repeat

Next the focus stacking loop begins.

- a photo is saved to SD card
- the `pycam.autofocus_vcm_step` is adjusted by the `FOCUS_STEPS` amount, which moves the lens farther from the sensor, thus bringing closer objects in focus&nbsp;

This repeats until the lens has moved to it's nearest focal position.

```auto
vcm_step = pycam.autofocus_vcm_step
vcm_step = min(255, vcm_step + FOCUS_STEPS)
  if vcm_step &lt; 255:
    pycam.capture_jpeg()
    pycam.tone(1600 + (vcm_step*10), 0.05)
    pycam.autofocus_vcm_step = vcm_step
    pycam.display_message(str(vcm_step), color=0xFF00FF)
    pycam.live_preview_mode()
    print("Now at focus", pycam.autofocus_vcm_step)
```

### Reset

When the focus stack has been captured, the MEMENTO resets the following:

- lens returns to its farthest focus distance
- exposure control is set back to auto
- gain control returns to auto
- white balance returns to auto

```auto
pycam.autofocus_vcm_step = FOCUS_START
pycam.camera.exposure_ctrl = True
pycam.set_camera_gain(None)  # go back to autogain
pycam.set_camera_wb(None)  # go back to autobalance
pycam.set_camera_exposure(None)  # go back to auto shutter
pycam.live_preview_mode()
```

# MEMENTO Focus Stacking

## Focus Stacking

Set your MEMENTO on a stable surface or tripod mount, compose your picture, and then press the shutter release button. It will start by taking a photo at the farthest focus, then adjust the lens by the lens voice motor coil increment you selected, and repeat until is has taken all the photos up to the nearest possible focus.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/684/medium800/camera_mementoFlowers.jpg?1707877826)

Remove the microSD card from the MEMENTO and place it into your computer's SD card reader.

Copy the images to a directory on your computer.

![camera_focus_frames.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/685/medium640/camera_focus_frames.jpg?1707877855)

Warning: 

One way to focus stack your images is with the web app [Focus Stacking Online](focusstackingonline.com)

Click the **Add** button and navigate to your image set.

Select the whole set and click **OK**.

Next, click the **Stack!** button and wait a few minutes for the images to be processed.

When it completes you can save your image by clicking the **Download** button.

![camera_fso1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/686/medium640/camera_fso1.jpg?1707878269)

![camera_fso2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/687/medium640/camera_fso2.jpg?1707878378)

![camera_fso3.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/688/medium640/camera_fso3.jpg?1707878429)

![camera_fso4.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/689/medium640/camera_fso4.jpg?1707878869)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/690/medium800/camera_fso5.jpg?1707878941)

## Options

You can use Photoshop to create focus stacked images using [this technique](https://creativecloud.adobe.com/cc/discover/article/focus-stacking-in-photoshop).

Another popular option is the cross-platform, open source [focus-stack](https://github.com/PetteriAimonen/focus-stack) by Petteri Aimonen.

# MEMENTO Focus Stacking

## Rack Focus

A rack focus is great for shifting attention between subjects without moving the camera. With a shallow depth of focus, the rack focus makes it clear where we should look, since blur quickly envelopes everything but the subject at the intended focal depth.

## Shoot a Rack Focus Shot

Set the MEMENTO on a stable surface or attach it to a tripod. Line up your subjects, and press the shutter release button.

The MEMENTO will snap pictures at the farthest to nearest depths.

![camera_figs_setup.jpg](https://cdn-learn.adafruit.com/assets/assets/000/127/692/medium640/camera_figs_setup.jpg?1707881877)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/695/medium800/camera_figrowthumbs.jpg?1707882284)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/693/medium800/camera_img0023.jpg?1707882145)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/694/medium800/camera_img0039.jpg?1707882169)

## Frames to GIFs

Remove the microSD card from the MEMENTO and insert it into an SD card reader on your computer. Copy the image files to a local directory. Then, you can turn the individual frames into a movie using video editing software, or stitch them into a GIF. [This page](https://learn.adafruit.com/adafruit-memento-camera-board/frames-to-gifs) shows you how to do just that!.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/691/medium800thumb/camera_figrow3.jpg?1707880190)

# MEMENTO Focus Stacking

## Split Focus

![](https://cdn-learn.adafruit.com/assets/assets/000/127/696/medium800thumb/camera_larslapy.jpg?1707882322)

We can create a type of "impossible" focus shot using just two photos. Similar to the macro shot focus stacking we did earlier, but this time using the two ideal frames and a simple matte.

## Far Focus

The first frame captures Lars in focus at the back of the totally not creepy abandoned warehouse.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/697/medium800/camera_lars_focus.jpg?1707883463)

## Near Focus

The second frame we'll use has Slappy in focus and Lars out of focus.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/698/medium800/camera_slappy_focus.jpg?1707883514)

## Matte

We'll use a matte (also called a mask or alpha channel) to composite the two images. In this matte, the white pixels represent full opacity of the top layer (Slappy in focus). The black pixels represent full transparency of the top layer, allowing the bottom layer (Lars in focus) to be seen.

![](https://cdn-learn.adafruit.com/assets/assets/000/127/699/medium800/camera_matteslap.jpg?1707883709)

Different image editing software will vary as far as the specifics, but the principle of compositing layers with a matte/mask/alpha channel will still apply.

You can see an example in Photoshop here.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/127/700/medium800/camera_larslappy_matted.jpg?1707884050)

![](https://cdn-learn.adafruit.com/assets/assets/000/127/701/medium800/camera_larslappy.jpg?1707884196)


## Featured Products

### MEMENTO - Python Programmable DIY Camera - Bare Board

[MEMENTO - Python Programmable DIY Camera - Bare Board](https://www.adafruit.com/product/5420)
Make memories, or just a cool camera-based project,&nbsp;with **Adafruit's MEMENTO Camera Board**. It's a development board with everything you need to create programmable camera and vision projects: with a camera module, TFT preview screen, buttons, SD card slot and...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5420)
[Related Guides to the Product](https://learn.adafruit.com/products/5420/guides)
### Adafruit MEMENTO Camera Enclosure & Hardware Kit

[Adafruit MEMENTO Camera Enclosure & Hardware Kit](https://www.adafruit.com/product/5843)
Once you've picked up your **MEMENTO Camera** and you're ready to take it out into the world, here is a chic and minimalist enclosure that will look great on the runways of Paris or the street photography of New York City! These front and back plates have been...

In Stock
[Buy Now](https://www.adafruit.com/product/5843)
[Related Guides to the Product](https://learn.adafruit.com/products/5843/guides)
### USB Type A to Type C Cable - approx 1 meter / 3 ft long

[USB Type A to Type C Cable - approx 1 meter / 3 ft long](https://www.adafruit.com/product/4474)
As technology changes and adapts, so does Adafruit. This&nbsp;&nbsp; **USB Type A to Type C** cable will help you with the transition to USB C, even if you're still totin' around a USB Type A hub, computer or laptop.

USB C is the latest industry-standard connector for...

In Stock
[Buy Now](https://www.adafruit.com/product/4474)
[Related Guides to the Product](https://learn.adafruit.com/products/4474/guides)
### SD/MicroSD Memory Card (8 GB SDHC)

[SD/MicroSD Memory Card (8 GB SDHC)](https://www.adafruit.com/product/1294)
Add mega-storage in a jiffy using this 8 GB class 4 micro-SD card. It comes with a SD adapter so you can use it with any of our shields or adapters. Preformatted to FAT so it works out of the box with our projects. Tested and works great with our <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1294)
[Related Guides to the Product](https://learn.adafruit.com/products/1294/guides)

## Related Guides

- [Adafruit MEMENTO Camera Board](https://learn.adafruit.com/adafruit-memento-camera-board.md)
- [Memento Photo Capture with itsaSNAP](https://learn.adafruit.com/memento-photo-capture-with-itsasnap.md)
- [MEMENTO Wireless Remote with TouchOSC](https://learn.adafruit.com/memento-wireless-remote.md)
- [ePaper Camera](https://learn.adafruit.com/epaper-camera.md)
- [Facial Detection and Recognition with MEMENTO](https://learn.adafruit.com/facial-detection-and-recognition-with-memento.md)
- [3D Printed Camera Case for MEMENTO](https://learn.adafruit.com/memento-3d-case.md)
- [BLE Cat Thermal Printer with MEMENTO](https://learn.adafruit.com/ble-cat-thermal-printer-with-memento.md)
- [IoT Bird Feeder with Camera](https://learn.adafruit.com/iot-window-bird-feeder-with-camera.md)
- [Remote Shutter Button for MEMENTO](https://learn.adafruit.com/memento-shutter.md)
- [OpenAI Image Descriptors with MEMENTO](https://learn.adafruit.com/openai-image-descriptors-with-memento.md)
- [MEMENTO Camera Quick Start Guide](https://learn.adafruit.com/memento-camera-quick-start-guide.md)
- [AdaBox 021](https://learn.adafruit.com/adabox021.md)
- [Face Tracking Robot with MEMENTO](https://learn.adafruit.com/face-tracking-robot-with-memento.md)
- [Set up Web Workflow on the Adafruit MEMENTO](https://learn.adafruit.com/set-up-web-workflow-on-the-adafruit-memento.md)
- [4x4 Rotary Encoder MIDI Messenger](https://learn.adafruit.com/4x4-rotary-encoder-midi-messenger.md)
