How to Resize an Image using Seam Carving Algorithm and Streamlit in Python

Hey, Everyone Today we are going to learn how to use Seam Carving Algorithm to resize an image, and also we will use Streamlit to provide a user-friendly interface.

Before diving in deeper, I will tell you all the things you need to install using pip:-

  • Seam Carving Algorithm:- pip install seam_carving
  • Pillow module of Python:- pip install Pillow
  • Numpy:- pip install numpy
  • Pathlib:- pip install pathlib
  • Streamlit:- pip install streamlit

Before going further let’s first check what is Streamlit and why we have used this:-

  • Streamlit is an open-source Python library that is used to create custom web apps. It is used as the frontend type in Python.
  • The main aim to use this is to provide the frontend look to our users.

What is Pillow in Python and why it is used?

It is the module in Python which is used to deal with the images like if we want to read an image, resize an image or transform an image.

Now the main discussion is over Seam Carving, what is it and how it works

Seam Carving:-

  1. It is an effective algorithm for image processing.
  2. It is used to resize the given image without losing the important parts/features of the image.
  3. The main idea behind the algorithm is we decrease the pixel(height or width) of the image by 1 at a time.
  4. The path connected from left to right with one pixel in each column is known as horizontal seam.
  5. The path connected from top to bottom with one pixel in each row is known as vertical seam.
  6. We need to find and remove the seam to do these things we need to follow the steps below.
  7. Energy Calculation: It is a measure of a pixel importance, if the energy we obtain is high, then the chance of including it  as part of a seam is very less. We have made use of the dual-gradient energy function.
  8. Seam identification. The next step is to find a vertical seam of minimum total energy(We have done this in the first step). This is exactly similar to find the shortest path in the given edged weighted graph.
  9. Seam removal. The final step is to remove from the image all of the pixels along the vertical or horizontal seam which you have found in the above steps.

Now let’s move to the streamlit part to create the frontend

streamlit.title(“Seam Carv”) is used to set the title of our web page.

uploaded_file = st.file_uploader(“Choose an image…”, type=”jpg”) provides a way to upload the image  and we store the that in uploaded_file variable

image = we are making use of the Pillow.Image to read the uploaded image file

streamlt.image(image, caption=’Uploaded Image.’, use_column_width=True) used to show the uploaded image to the web page.

Now we are making a new dir to store the uploaded images.

After that, we are making use of the inbuilt seam_carving module to carve the uploaded image. One more thing here we are decreasing the quality of the uploaded image.

And at last, we just show the result to the user by making use of streamlit. image().

To run the:-

use streamlit run [name of the file].py

Here is the complete code:-

import streamlit as st
import os
import numpy as np
from PIL import Image
from pathlib import Path
import seam_carving

st.title("Seam Carving ")

uploaded_file = st.file_uploader("Choose an image...", type="jpg")
if uploaded_file is not None:
    image =
    st.image(image, caption='Uploaded Image.', use_column_width=True)
    directory = "tempDir"
    path = os.path.join(os.getcwd(), directory)
    p = Path(path)
    if not p.exists():
    with open(os.path.join(path,,"wb") as f:
    file_loc = os.path.join(path,  
    origin ='demo.png',quality = 50,optimize=True)
    src = np.array(origin)
    src_h, src_w, _ = src.shape
    dst = seam_carving.resize(
        src, (src_w - (0.3*src_w), src_h-(0.3*src_h)),
        energy_mode='forward',   # Choose from {backward, forward}
        order='height-first',  # Choose from {width-first, height-first}

    im = Image.fromarray(dst)
    st.image(im, caption='Uploaded Image.', use_column_width=True)

Here is the output we got:-

Resize an Image using Seam Carving Algorithm and Streamlit in Python

Resize an Image using Seam Carving Algorithm and Streamlit in Python

11 responses to “How to Resize an Image using Seam Carving Algorithm and Streamlit in Python”

  1. Tushar Jaan says:

    Thank you!
    That was really helpful and easy way to resize an image.
    Keep it up!

  2. Mihir kumar says:

    Good work as always shrimad.. keep it up!

  3. Shashank says:

    Great work Shrimad ! That was really helpful and easy way to resize an image.

  4. Vinutha says:

    Nice… Keep it up srimad.. keep exploring

  5. Vinutha says:

    Nice… Keep it up srimad.. keep exploring.. keep coding

  6. Suraj Jha says:

    Nicely Explained.
    The code is compact and beautiful.

  7. Anshu says:

    Well explained and very informative.
    Nice work. Keep posting.

  8. Preetam says:

    Really helpful…
    Nice work bro, keep it up..

  9. Rahul says:

    Awesome, good job shrimad. Keep going as always!

  10. Aayush Neupane says:

    You are superb bhaiya! Keep it up!
    It was really awesome blog.

  11. Swetha says:

    That was very helfull and Informative, Thank you and keep going.

Leave a Reply

Your email address will not be published. Required fields are marked *