Skip to content

cjrh/sembra

Repository files navigation

Sembra

Seam carving: content-aware image resizing.

About

Seam carving is a content-aware image resizing technique that resizes an image by removing or adding pixels in the least noticeable areas. The technique was first developed by Shai Avidan and Ariel Shamir in 2007. The technique was later improved by Michael Rubinstein, Ariel Shamir, and Shai Avidan in 2008.

The rust code in this repository is a port of the Python code by Jiahao Li. Fun fact, the port was done by the o1 model from OpenAI. It was nearly correct, and only two small bugs required fixing.

CLI Usage

$ sembra --help
CLI for our seam carving demo

Usage: sembra [OPTIONS] --input <INPUT> --output <OUTPUT>

Options:
      --input <INPUT>              Input image path
      --output <OUTPUT>            Output image path
      --width <WIDTH>              Target width
      --height <HEIGHT>            Target height
      --energy-mode <ENERGY_MODE>  Energy mode: "backward" or "forward" [default: backward]
      --order <ORDER>              Order mode: "width-first" or "height-first" [default: width-first]
      --keep-mask <KEEP_MASK>      Keep mask image path (optional)
      --drop-mask <DROP_MASK>      Drop mask image path (optional)
      --step-ratio <STEP_RATIO>    Step ratio for expansions [default: 0.5]
  -h, --help                       Print help
  -V, --version                    Print version

Examples

Generally speaking, you get the best results if you go smaller. It is quite impressive how well this works. Here is an example of resizing a rectangular image down into a square. We'll use the famous painting Nymphs and Satyr (1873) by William-Adolphe Bouguereau:

$ sembra --input nes.jpg --output nes_big_square.jpg \
    --width 350 --height 350 --energy-mode forward
Original

Original 350x500

Resized smaller

Reduced 350x350

We turned a rectangular image into a square with very little observable distortion! Quite remarkable.

You can also go bigger, but the results are not as good. Here is an example of the same image, but this time we enlarge the width to make it square:

$ sembra --input nes.jpg --output nes_big_square.jpg \
    --width 500 --height 500 --energy-mode forward
Original

Original 350x500

Resized larger

Enlarged 500x500

While there is clearly some distortion, there is also excellent preservation of some of the more detailed parts of the image. This is what seam carving gives you. You can see this by comparing the seam-carving enlargement versus what you get from a typical image resize (resampling) in an image editor:

Original

Seam-carved up 500x500

Resized larger

Resampling 500x500

Side-by-side, you can clearly see (may need to zoom the page) that the seam-carving enlargment has preserved detail in key areas, like faces, fingers, eyes, and so on. Of course, this comes at the cost greater distortion in other less detailed areas.

About

Seam carving image resizing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages