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.
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
Axis at both sides of the Image can be removed using:
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.
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.
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
Merging Two Images
PILImage.Image.paste() — method is used to paste an image on another image. The Paste method takes two images as the parameter.
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.
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.