🧪 We're running a cheminformatics notebook competition!

Enter by October 4
29 / 103
Seam Carving

Seam Carving

by marimo team

Content-aware image resizing that preserves important features using dynamic programming.

About this notebook

This notebook implements the seam carving algorithm in Python with NumPy, scikit-image, and Numba. The input is Salvador Dali's painting The Persistence of Memory, which the notebook downloads once and reads with scikit-image. Seam carving reduces the width of an image while it keeps the shapes of the main content, so the clocks in the painting do not deform.

A slider sets the amount of resizing, from 70 to 100 percent of the original width. When you move the slider, marimo re-runs the carving cell and shows the original image next to the carved result. The cell also prints how many seconds the carve took.

The algorithm converts the image to grayscale, builds an energy map from horizontal and vertical Sobel filters, and finds the lowest-energy vertical seam with dynamic programming. It removes one seam at a time until the image reaches the target width. Numba compiles the inner loops, and marimo's cache stores the result for each slider value, so a value you already computed returns at once.

Related notebooks