Socket
Socket
Sign inDemoInstall

seam-carving

Package Overview
Dependencies
8
Maintainers
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    seam-carving

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


Maintainers
1

Readme

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.

Installation

Install a stable version from PyPI.

pip install seam-carving

Or install the latest version from GitHub.

pip install git+https://github.com/li-plus/seam-carving.git@master

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(Image.open('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}
    keep_mask=None
)
Image.fromarray(dst).show()

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

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

For more examples, please refer to example/demo.py.

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).

Benchmarks

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.

MethodsBE -200pxBE +200pxFE -200pxFE +200px
vivianhylee/seam-carving192.63217.04109.33108.53
sameeptandon/python-seam-carving91.67124.21N/AN/A
andrewdcampbell/seam-carving91.3890.8998.47102.84
dharness/seam_carving59.8173.04N/AN/A
Ours1.121.141.131.22

References

  • Avidan, S., & Shamir, A. (2007). Seam carving for content-aware image resizing. In ACM SIGGRAPH 2007 papers (pp. 10-es). [paper] [blog]
  • Rubinstein, M., Shamir, A., & Avidan, S. (2008). Improved seam carving for video retargeting. ACM transactions on graphics (TOG), 27(3), 1-9. [paper]
  • Das, A. (2019). Improved seam carving with forward energy. [blog]

Keywords

FAQs


Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc