A super-fast Python implementation of seam carving algorithm for intelligent image resizing.
Project description
Seam Carving
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.
| 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 | 1.12 | 1.14 | 1.13 | 1.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]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file seam-carving-1.1.0.tar.gz.
File metadata
- Download URL: seam-carving-1.1.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e23c0cfef668c707b6157c15b4c42b4fa7be898fb997292bbcd852c7c42a1450
|
|
| MD5 |
26dccb5716672029ff327989d3df80f3
|
|
| BLAKE2b-256 |
0df00a5df7ac8498dd795a6cddc93e1c5f47deaf32bf659fa8cb24923a343913
|
File details
Details for the file seam_carving-1.1.0-py3-none-any.whl.
File metadata
- Download URL: seam_carving-1.1.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
178637e060500b267eff651db0df11b144eb89b24f8228c105d5b664613573e4
|
|
| MD5 |
534e24a69c205c7e6710a0ce7444c6c0
|
|
| BLAKE2b-256 |
e4adecfbde3720752af1f2e4884beff0f21113f25803162fcd45612ef64e8dd7
|