Resize an Image using Seam Carving Algorithm and Streamlit
Last Updated :
28 Apr, 2025
As images continue to dominate our online and offline lives, the need to resize them to meet specific requirements has increased. While conventional resizing techniques such as cropping and scaling are effective, they can lead to distortion and loss of important details. Seam carving, on the other hand, is an algorithmic approach that resizes images while preserving the essential features.
In this article, we will explore how to resize an image using the seam carving algorithm and Streamlit in Python.
Seam CarvingĀ
Seam carving is a content-aware image resizing algorithm that intelligently identifies and removes the least important pixels in an image. Instead of cropping or scaling the image, seam carving selectively removes vertical or horizontal seams of pixels with the least energy, thus preserving the important features of the image.
Seam carving has several advantages over conventional resizing techniques. For one, it allows for non-uniform resizing, which means you can resize an image along its width or height independently. Additionally, it preserves the aspect ratio of the image and reduces distortion, ensuring that the resized image looks natural.
Using Streamlit in Python
Streamlit is an open-source Python library that enables you to create interactive data science applications. It offers a simple and intuitive interface for building web applications, making it an excellent choice for our image resizing project.
To get started, you need to have Python installed on your computer, along with the following libraries:
You can install these libraries using pip, a Python package installer, by running the following command in your terminal:
pip install numpy opencv-python streamlit
Once you have installed the required libraries, you can start building your image-resizing application.
Resizing Images Using Seam Carving and Streamlit
To resize an image using seam carving and Streamlit, you need to perform the following steps:
1. Import the necessary libraries:
Python3
import numpy as np
import cv2
import streamlit as st
2. Create a Streamlit app:
Python3
st.set_page_config(page_title="Seam Carving Image Resizing")
st.title("Seam Carving Image Resizing")
3. Allow the user to upload an image:
Python3
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
4. Load the image and display it:
Python3
if uploaded_file is not None:
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
st.image(img, channels="BGR")
5. Allow the user to specify the new size of the image:
Python3
new_width = st.slider("New Width", min_value=1, max_value=img.shape[1], value=img.shape[1])
new_height = st.slider("New Height", min_value=1, max_value=img.shape[0], value=img.shape[0])
6. Resize the image using seam carving:
Python3
def seam_carve(img, new_width, new_height):
for i in range(img.shape[1] - new_width):
energy_map = np.abs(cv2.Scharr(img, -1, 1, 0)) \
+ np.abs(cv2.Scharr(img, -1, 0, 1))
energy_map = energy_map.astype(np.float64)
min_energy_map = np.zeros_like(energy_map)
min_energy_map[0] = energy_map[0]
for row in range(1, img.shape[0]):
for col in range(img.shape[1]):
if col == 0:
min_energy_map[row, col] = energy_map[row, col] \
+ min(min_energy_map[row - 1, col],
min_energy_map[row - 1, col + 1])
elif col == img.shape[1] - 1:
min_energy_map[row, col] = energy_map[row, col] \
+ min(min_energy_map[row - 1, col - 1],
min_energy_map[row - 1, col])
else:
min_energy_map[row, col] = energy_map[row, col] \
+ min(min_energy_map[row - 1, col - 1],
min_energy_map[row - 1, col],
min_energy_map[row - 1, col + 1])
seam_mask = np.ones_like(img[:, :, 0])
col = np.argmin(min_energy_map[-1])
for row in reversed(range(img.shape[0])):
seam_mask[row, col] = 0
if col == 0:
col = np.argmin(min_energy_map[row - 1, col:col + 2])
elif col == img.shape[1] - 1:
col = np.argmin(min_energy_map[row - 1, col - 1:col + 1]) \
+ col - 1
else:
col = np.argmin(min_energy_map[row - 1, col - 1:col + 2]) \
+ col - 1
img = cv2.resize(img, (new_width, new_height))
seam_mask = cv2.resize(seam_mask, (new_width, new_height))
for channel in range(img.shape[2]):
img[:, :, channel] = np.multiply(img[:, :, channel], seam_mask)
img = img.astype(np.uint8)
return img
7. Display the resized image:
Python3
if uploaded_file is not None:
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
st.image(seam_carve(img, new_width, new_height), channels="BGR")
Now combine all these steps and make one file that can generate output as required.
Final Code
Python3
import cv2
import numpy as np
import streamlit as st
def seam_carve(img, new_width, new_height):
img = img.astype(np.float64)
for i in range(int(img.shape[1] - new_width)):
energy_map = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2GRAY)
energy_map = cv2.Sobel(energy_map, cv2.CV_64F, 1, 0) ** 2 + \
cv2.Sobel(energy_map, cv2.CV_64F, 0, 1) ** 2
min_energy_map = np.zeros_like(energy_map)
min_energy_map[0] = energy_map[0]
for row in range(1, energy_map.shape[0]):
for col in range(energy_map.shape[1]):
if col == 0:
min_energy_map[row, col] = energy_map[row, col] + \
min(min_energy_map[row - 1, col],
min_energy_map[row - 1, col + 1])
elif col == energy_map.shape[1] - 1:
min_energy_map[row, col] = energy_map[row, col] + min(
min_energy_map[row - 1, col - 1],
min_energy_map[row - 1, col])
else:
min_energy_map[row, col] = energy_map[row, col] + min(
min_energy_map[row - 1, col - 1],
min_energy_map[row - 1, col],
min_energy_map[row - 1, col + 1])
seam_mask = np.ones_like(img[:, :, 0])
col = np.argmin(min_energy_map[-1])
for row in reversed(range(img.shape[0])):
seam_mask[row, col] = 0
if col == 0:
col = np.argmin(min_energy_map[row - 1, col:col + 2])
elif col == img.shape[1] - 1:
col = np.argmin(
min_energy_map[row - 1, col - 1:col + 1]) + col - 1
else:
col = np.argmin(
min_energy_map[row - 1, col - 1:col + 2]) + col - 1
img = cv2.resize(img, (new_width, new_height))
seam_mask = cv2.resize(seam_mask, (new_width, new_height))
for channel in range(img.shape[2]):
img[:, :, channel] = np.multiply(img[:, :, channel], seam_mask)
img = img.astype(np.uint8)
return img
st.title("Seam Carving Image Resizing")
uploaded_file = st.file_uploader(
"Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
img = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
st.image(img, clamp=True, channels="RGB")
new_width = st.slider("New Width", 100, img.shape[1], img.shape[1])
new_height = st.slider("New Height", 100, img.shape[0], img.shape[0])
st.image(seam_carve(img, new_width, new_height),
clamp=True, channels="RGB")
Output :
1. first we need to make one .py file and add the above code to that file.
2. run this code and go to the command prompt and write the below command
streamlit run "FilePathInComputer"
Terminal OUTPUT
so this command will start a local server, we can see that through the above IP in the browser which looks like the below.
url OUTPUT
now using Browse Files Option we have to select the image which we want to resize.
image picking
after that change width and height slider as required for the new image and press the run button on the top right corner available menu so this will give the new width and height, resized image as below.
Ā
Final OUTPUT
So this way we can resize an Image using Seam Carving Algorithm and Streamlit in Python.
Similar Reads
Image resizing using Seam carving using OpenCV in Python Seam carving is an effective image processing technique with the help of which an image can be resized without removing important elements from the image. The basic approach is to find all the continuous pixels with low energy from left to right or from top to bottom. After the region is selected, i
2 min read
Python | Foreground Extraction in an Image using Grabcut Algorithm In this article we'll discuss an efficient method of foreground extraction from the background in an image. The idea here is to find the foreground, and remove the background. GrabCut Algorithm for Image SegmentationGrabCut is an interactive image segmentation algorithm that was introduced by Carst
8 min read
Image Processing Algorithms in Computer Vision In the field of computer vision, image preprocessing is a crucial step that involves transforming raw image data into a format that can be effectively utilized by machine learning algorithms. Proper preprocessing can significantly enhance the accuracy and efficiency of image recognition tasks. This
10 min read
Cropping an Image in a circular way using Python In this article, we will learn to crop an image circularly using a pillow library. Cropping an image circularly means selecting a circular region inside an image and removing everything outside the circle. Approach: If you have an L mode image, the image becomes grayscale. So we create a new image
2 min read
Add image to a live camera feed using OpenCV-Python In this article, we are going to learn how to insert an image in your live camera feed using OpenCV in Python. Stepwise ImplementationStep 1: Importing the libraries CV reads and stores all the images as a NumPy array. We need the NumPy library to manipulate the image and as expected we need the cv2
4 min read
How to Crop an Image using the Numpy Module? In this article, we are going to learn about the most naive and efficient approach to crop an image without using any additional module. The numpy module is a Python library used for working with arrays, and large data sets. Python does not have any native support for arrays, as opposed to other hig
4 min read
Extracting patches from large images using Python In this article, we are going to look at how we can extract patches from large images in Python. We will be using the patchify library to extract patches from images. Patchy is a Python library that can split images into small overlap able patches by given patch cell size, and merge patches into the
2 min read
Image Segmentation using Python's scikit-image module The process of splitting images into multiple layers, represented by a smart, pixel-wise mask is known as Image Segmentation. It involves merging, blocking, and separating an image from its integration level. Splitting a picture into a collection of Image Objects with comparable properties is the fi
13 min read
Erosion and Dilation of images using OpenCV in python Morphological operations are a set of operations that process images based on shapes. They apply a structuring element to an input image and generate an output image. The most basic morphological operations are two: Erosion and Dilation Basics of Erosion: Erodes away the boundaries of the foreground
2 min read
Image Processing in Java - Creating a Mirror Image Prerequisite:Â Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
3 min read