×

Image Processing with STREAMLIT

Image Processing Streamlit Feature

In this article, we will see how we can use Streamlit with Image Processing Techniques. Assuming that you have Streamlit installed and working on your system.

How to Load an image?

import streamlit as st
from PIL import Image
import matplotlib.pyplot as plt
  • PIL (Python Imaging Library) is a Python lIbrary to work with images. The Image module of PIL have various funtion to manipulate the images — Image.open() is one of the various function provided by Image module, to load an image.
image = Image.open("Capture.PNG") #Image name
fig = plt.figure()
plt.imshow(image)
st.pyplot(fig)
Output
Load Image In Streamlit

Axis at both sides of the Image can be removed using:

plt.axis("off")
image = Image.open("Capture.PNG") #Image name
fig = plt.figure()
plt.imshow(image)
plt.axis("off")
st.pyplot(fig)
Output
Load Without Axis Streamlit

Rotating the Image

Image module of PIL have rotate() method to rotate the image. The rotate() method of Python Image Processing Library takes a number of degrees as a parameter and rotates the image in a counter-clockwise direction to the number of degrees specified.

image = Image.open("Capture.PNG") 
rotated_img = image.rotate(180) #Number of degree's

fig = plt.figure()
plt.imshow(rotated_img)
plt.axis("off")
st.pyplot(fig)

Output

Rotated Image Streamlit

Creating Thumbnail

PIL Image.thumbnail() — Method allows us to convert the image into a thumbnail. The thumbnail() – method modifies the image to contain a thumbnail version of itself, no larger than the given size.

The Thumbnail method calculates an appropriate thumbnail size to preserve the aspect of the image, calls the draft() – method to configure the file reader (where applicable), and finally resizes the image.

image = Image.open("Capture.PNG") 

size = (100, 100) #Heigth and width
image.thumbnail(size)

fig = plt.figure()
plt.imshow(image)
plt.axis("off")
st.pyplot(fig)
Output
Thumbnail Streamlit

Cropping the image

PIL Image.crop() — method crops a rectangular portion of any image. The crop method takes a tuple as a parameter, defining the left, top, right, and bottom coordinates of the Image.

The top left coordinates correspond to (x, y) = (left, upper), and the bottom right coordinates correspond to (x, y) = (right, lower)

image = Image.open("Capture.PNG") 

#Coordinates
left = 100
top = 150
right = 500
bottom = 400 

image = image.crop((left, top, right, bottom))

fig = plt.figure()
plt.imshow(image)
plt.axis("off")
st.pyplot(fig)
Output
Crop Image Streamlit

Merging Two Images

PIL Image.Image.paste() — method is used to paste an image on another image. The Paste method takes two images as the parameter.

#First Image
image_one = Image.open("Capture.PNG") 

#Second Image
image_two = Image.open("github.PNG")

Image.Image.paste(image_one, image_two)

fig = plt.figure()
plt.imshow(image_one)
plt.axis("off")
st.pyplot(fig)
Output
Paste Image Streamlit

paste() method also takes an optional parameter called box — A tuple giving the X & Y – coordinates to paste the image at specific position. If box parameter is omitted, the image will be pasted at the upper left corner.

#First Image
image_one = Image.open("Capture.PNG") 

#Second Image
image_two = Image.open("github.PNG")

Image.Image.paste(image_one, image_two, (400, 100)) #Box parameter

fig = plt.figure()
plt.imshow(image_one)
plt.axis("off")
st.pyplot(fig)
Output
Box Parameter Streamlit

Flipping the images

PIL image.transpose() — Method used to flip images. Transpose method takes parameters as follows:

  • FLIP_LEFT_RIGHT : Flip Left Right parameter creates the mirror image of the actual image.
  • FLIP_TOP_BOTTOM: Flip Top Bottom parameter rotates the image by 180 degrees.
image = Image.open("Capture.PNG") 
fig = plt.figure()

#First Image
image_one = image.transpose(Image.FLIP_LEFT_RIGHT)
plt.subplot(1, 2, 1)
plt.imshow(image_one)

#Second Image
image_two = image.transpose(Image.FLIP_TOP_BOTTOM)
plt.subplot(1, 2, 2)
plt.imshow(image_two)

st.pyplot(fig)
  • Used Subplot to load the images side by side.
Output
Flip Image Streamlit 1

Black & White image

PIL image.convert() — Convert method is particularly used to achieve a truly B&W or grayscale image. The convert method transforms the image to one of the colour modes. Image modes are as follows:

  •  L : L stands for Luminance. L image mode has one channel that can take any value between 0 and 255 representing white, black and all the shades of gray in between. It’s an 8-bit grayscale image mode.
  • LA : LA mode is used to achieve grayscale images with transparency. Only png and gif image file types support transparency channel.
  •  1 : 1 image mode is true black & white. 1 image mode has only one channel which takes two value representing full white or full black.
image = Image.open("Capture.PNG") 
fig = plt.figure()

image = image.convert("1")
plt.imshow(image)

st.pyplot(fig)
Output
Balck And White Image Streamlit

Adding Filter’s to the Images

The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method.

Contour

image = Image.open("Capture.PNG") 
fig = plt.figure()

con = image.filter(ImageFilter.CONTOUR)
plt.imshow(con)

st.pyplot(fig)
Output
Contour Filter Streamlit

Emboss

image = Image.open("Capture.PNG") 
fig = plt.figure()

emb = image.filter(ImageFilter.EMBOSS)
plt.imshow(emb)

st.pyplot(fig)
Output
Emboss Filter Streamlit

Edge Enhance

image = Image.open("Capture.PNG") 
fig = plt.figure()

enh = image.filter(ImageFilter.EDGE_ENHANCE)
plt.imshow(enh)

st.pyplot(fig)
Output
Edge Enhance Filter Streamlit

Contrast

image = Image.open("Capture.PNG") 
fig = plt.figure()

contrast = ImageEnhance.Contrast(image).enhance(12)
plt.imshow(contrast)

st.pyplot(fig)
Output
Contrast Filter Streamlit