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:-
- It is an effective algorithm for image processing.
- It is used to resize the given image without losing the important parts/features of the image.
- The main idea behind the algorithm is we decrease the pixel(height or width) of the image by 1 at a time.
- The path connected from left to right with one pixel in each column is known as horizontal seam.
- The path connected from top to bottom with one pixel in each row is known as vertical seam.
- We need to find and remove the seam to do these things we need to follow the steps below.
- 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.
- 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.
- 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 = Image.open(uploaded_file) 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 = Image.open(uploaded_file) 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(): os.mkdir(p) with open(os.path.join(path, uploaded_file.name),"wb") as f: f.write(uploaded_file.getbuffer()) file_loc = os.path.join(path, uploaded_file.name) origin = Image.open(uploaded_file) origin.save('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} keep_mask=None ) im = Image.fromarray(dst) st.image(im, caption='Uploaded Image.', use_column_width=True)
Here is the output we got:-
Thank you!
That was really helpful and easy way to resize an image.
Keep it up!
Good work as always shrimad.. keep it up!
Great work Shrimad ! That was really helpful and easy way to resize an image.
Nice… Keep it up srimad.. keep exploring
Nice… Keep it up srimad.. keep exploring.. keep coding
Nicely Explained.
The code is compact and beautiful.
Well explained and very informative.
Nice work. Keep posting.
Really helpful…
Nice work bro, keep it up..
Awesome, good job shrimad. Keep going as always!
Good work!!
Keep working on new ideas 🙂
Great work!
Nicely done Srimad, keep up the good work.
Great Job Shrimad
Perfectly Written and Explained
Keep Going…!
Nice, easy and clear explanation for the code,and also good material
Well,it’s been the most informative blog I have come across as a fresher …great one buddy!!!
Good explanation…. Very informative.. loved it..
You are superb bhaiya! Keep it up!
It was really awesome blog.
Very detailed and easy to follow, all eyes set on the upcoming works…
Good work, waiting for more such posts.
That was very helfull and Informative, Thank you and keep going.