Skip to main content

A super-fast Python implementation of seam carving algorithm for intelligent image resizing.

Project description

Seam Carving

PyPI Unit Test License: MIT codecov

A super-fast Python implementation of Seam carving for content-aware image resizing, and the forward energy function proposed in Improved seam carving for video retargeting.

With seam carving algorithm, the image could be intelligently resized while keeping the important contents undistorted. The carving process could be further guided, so that an object could be removed from the image without apparent artifacts.


Install a stable version from PyPI.

pip install seam-carving

Or install the latest version from GitHub.

pip install git+

Quick Start

To scale an image, use seam_carving.resize method.

import numpy as np
from PIL import Image
import seam_carving

src = np.array('fig/castle.jpg'))
src_h, src_w, _ = src.shape
dst = seam_carving.resize(
    src, (src_w - 200, src_h),
    energy_mode='backward',   # Choose from {backward, forward}
    order='width-first',  # Choose from {width-first, height-first}

To remove an object from an image, use seam_carving.remove_object method.

src = np.array('fig/beach.jpg'))
mask = np.array('fig/beach_girl.png').convert('L'))
dst = seam_carving.remove_object(src, drop_mask=mask, keep_mask=None)

For more examples, please refer to example/

Example Results

Scaling Up & Down

Resizing along the x-axis using original backward energy function.

Backward Energy vs Forward Energy

Reduce the width of the bench image using backward & forward energy function.

Aspect Ratio Change

The image width and height could be changed simultaneously. The order of vertical and horizontal seams has little to do with the final results. Currently we only support two kinds of seams-order: width-first and height-first. In width-first mode, we remove/insert all vertical seams first, and then the horizontal ones, while height-first is the opposite.

Object Protection

The protected mask is free from seam removal and insertion.

Object Removal

Specify an object mask to remove (red) and a mask to protect (green, optional).


We compare the performance of our implementation and other popular Python repos on castle.jpg. The image is narrowed or widened by 200 pixels using backward energy (BE) or forward energy (FE), respectively. Below is the running time (second) evaluated on a MacBook Pro.

Methods BE -200px BE +200px FE -200px FE +200px
vivianhylee/seam-carving 192.63 217.04 109.33 108.53
sameeptandon/python-seam-carving 91.67 124.21 N/A N/A
andrewdcampbell/seam-carving 91.38 90.89 98.47 102.84
dharness/seam_carving 59.81 73.04 N/A N/A
Ours 2.63 3.54 5.34 6.23


  • Avidan, Shai, and Ariel Shamir. "Seam carving for content-aware image resizing." ACM SIGGRAPH 2007 papers. 2007. 10-es. [paper]
  • Rubinstein, Michael, Ariel Shamir, and Shai Avidan. "Improved seam carving for video retargeting." ACM transactions on graphics (TOG) 27.3 (2008): 1-9. [paper]
  • Avik Das. "Improved seam carving with forward energy." [link]

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seam-carving-1.0.0.tar.gz (9.8 kB view hashes)

Uploaded source

Built Distribution

seam_carving-1.0.0-py3-none-any.whl (10.1 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page