Pillow is a fork for the Python Imaging Library, PIL, that provides a variety of image-processing features to manipulate images and apply different filters to them. It is a powerful library that is easy to integrate with other Python libraries and frameworks. By using the pillow library, we can perform various operations on an image as follows:
Filter effects like blurring
Image enhancement, like adjusting brightness, contrast, and sharpness
Image overlay, like adding text on an image
In this Answer, we shall enhance the image by changing its brightness, contrast, and sharpness and observe how it changes.
We will use the PIL
library to manipulate the image and then use matplotlib
to display the results once the code is executed successfully.
import matplotlib.pyplot as pltfrom PIL import Image, ImageEnhanceimport requestsfrom io import BytesIO
matplotlib
: To create visualizations and plots in the Python library.
PIL
: Computer vision library to apply operations on images.
Image
: To enhance the image properties that change its appearance.
ImageEnhance
: To access different filters, including blur.
requests
: To send the requests over the HTTP server to the website.
BytesIO
: To handle the binary data as an in-memory system.
This is a sample code that is used to adjust the image properties. We can add the code for each image-enhancing property to achieve the desired output.
import matplotlib.pyplot as pltfrom PIL import Image, ImageEnhanceimport requestsfrom io import BytesIOimage_url = ""#Fetch imageresponse = requests.get(image_url)image_data = BytesIO(response.content)original_image = Image.open(image_data)#Add the Image enhance code herebrightened_image = enhancer.enhance(3.0)plt.figure(figsize=(10, 5))plt.subplots_adjust(wspace=0.4, hspace=0.1)plt.subplot(1, 2, 1)plt.imshow(original_image)plt.axis('off')plt.title('Original Image')plt.subplot(1, 2, 2)plt.imshow(enhanced_image)plt.axis('off')plt.title('Enhanced Image')plt.show()
Lines 1–4: Import the required libraries and modules.
Line 6: Store the link of the image that is to be used on image_url
. We can also use a local image file and give its exact name.
Lines 9–11: Use request
to fetch the image from the server and open it using the Image.open()
method and pass the converted image to it.
Line 14: Use the enhance()
method and specify the enhancement factor value as a parameter. The higher the value, the more the effect of the applied image enhancer.
Lines 16–17: Specify the figure size that is to appear in the plot and assign the grid size.
Lines 19–22: Display the image using imshow()
and pass the original image to it, turn off the x-axis labels, and specify the title in the title()
.
Lines 24–27: Display the image using imshow()
and pass the flipped image to it, turn off the x-axis labels, and specify the title in the title()
.
Line 29: Show the resultant plot.
Now let's use this sample code to change the image's brightness, contrast, and sharpness.
Brightness refers to the overall darkness or lightness of the image that is done by changing the luminance of the pixels. Each pixel's intensity value is changed equally between the range of 0 to 255, so to do this, a constant value is either added to each pixel if we want to increase the brightness or subtracted from each pixel if we want to decrease the brightness.
The image's brightness is increased; therefore, the image appears comparatively lighter than the original image.
Let's code this concept and change the brightness of an image.
in this example, we fetch an online image through its link address and increase the brightness by a factor of 3.0
using Brightness()
from ImageEnhance
.
import matplotlib.pyplot as plt from PIL import Image, ImageEnhance import requests from io import BytesIO image_url = "https://images.pexels.com/photos/3685271/pexels-photo-3685271.jpeg?auto=compress&cs=tinysrgb&w=300" # Fetch image response = requests.get(image_url) image_data = BytesIO(response.content) original_image = Image.open(image_data) # Enhance brightness enhancer = ImageEnhance.Brightness(original_image) brightened_image = enhancer.enhance(3.0) plt.figure(figsize=(10, 5)) plt.subplots_adjust(wspace=0.4, hspace=0.1) plt.subplot(1, 2, 1) plt.imshow(original_image) plt.axis('off') plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(brightened_image) plt.axis('off') plt.title('Brightened Image') plt.show()
Contrast refers to changing the intensity of the pixels where the dark pixels are darker and the light pixels are lighter. Each pixel is changed to redefine the range of the pixel values in order to increase or decrease the intensity range of the image. To increase the contrast, we expand the range, and to decrease the contrast, we compress the range.
The image's contrast is increased; therefore, the image appears comparatively more defined than the original image.
Let's code this concept and change the contrast of the image.
in this example, we fetch an online image through its link address and increase the contrast by a factor of 3.0
using Contrast()
from ImageEnhance
.
import matplotlib.pyplot as plt from PIL import Image, ImageEnhance import requests from io import BytesIO image_url = "https://images.pexels.com/photos/5430814/pexels-photo-5430814.jpeg?auto=compress&cs=tinysrgb&w=300" # Fetch image response = requests.get(image_url) image_data = BytesIO(response.content) original_image = Image.open(image_data) #Increase contrast enhancer = ImageEnhance.Contrast(original_image) contrast_image = enhancer.enhance(3.0) plt.figure(figsize=(10, 5)) plt.subplots_adjust(wspace=0.4, hspace=0.1) plt.subplot(1, 2, 1) plt.imshow(original_image) plt.axis('off') plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(contrast_image) plt.axis('off') plt.title('Contrasted Image') plt.show()
Sharpness refers to improving the image's contrast, specifically at the edges, and defining the details. The value of the pixels is multiplied by the kernel value to enhance the image areas with visible contrast differences to make the image more pronounced.
The sharpness of the image is increased; therefore, the image appears comparatively to have more defined outlines and texture than the original image.
Let's code this concept and change the sharpness of the image.
in this example, we fetch an online image through its link address and increase the sharpness by a factor of 4.5
using Sharpness()
from ImageEnhance
.
import matplotlib.pyplot as plt from PIL import Image, ImageEnhance import requests from io import BytesIO image_url = "https://images.pexels.com/photos/2313396/pexels-photo-2313396.jpeg?auto=compress&cs=tinysrgb&w=300" # Fetch image response = requests.get(image_url) image_data = BytesIO(response.content) original_image = Image.open(image_data) #Enhance Sharpness enhancer = ImageEnhance.Sharpness(original_image) sharpened_image = enhancer.enhance(4.5) plt.figure(figsize=(10, 5)) plt.subplots_adjust(wspace=0.4, hspace=0.1) plt.subplot(1, 2, 1) plt.imshow(original_image) plt.axis('off') plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(sharpened_image) plt.axis('off') plt.title('Sharpened Image') plt.show()
There are a lot of real-life scenarios where there is a need to enhance the image. Let's take a look at a few of the scenarios where image enhancement is useful.
Can we decrease the enhancements of the images using the same code?
Free Resources