Python Pillow – Unsharp Mask Filter

Python Pillow – Unsharp Mask Filter ”; Previous Next Unsharp masking is a widely used image sharpening technique in image processing. The fundamental concept behind unsharp masking involves using a softened or unsharp version of a negative image to create a mask for the original image. This unsharp mask is subsequently merged with the original positive image, resulting in a less blurry version of the original image, making it clearer. The Python pillow(PIL) library provides a class called UnsharpMask() within its ImageFilter module for the application of the Unsharp Masking filter to images. The UnsharpMask Filter The ImageFilter.UnsharpMask() class represents an unsharp mask filter, which is used to enhance image sharpness. Following is the syntax of the ImageFilter.UnsharpMask() class − class PIL.ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3) Where, radius − This parameter controls the blur radius. Radius affects the size of the edges to be enhanced. A smaller radius sharpens smaller details, while a larger radius can create light halos around objects. Adjusting the radius and amount affects each other; decreasing one allows the other to have a greater impact. percent − It determines the strength of the unsharp mask in percentage. Percentage controls the strength or magnitude of the sharpening effect. It determines how much contrast is added at the edges of objects. A higher amount results in a more pronounced sharpening effect, making edge borders darker and lighter. It does not impact the width of the edge rims. threshold − Threshold controls the minimum change in brightness that gets sharpened. It decides how different adjacent tonal values need to be for the filter to take action. A higher threshold prevents smooth areas from becoming speckled. Lower values sharpen more, while higher values spare lower-contrast areas. Example The following example applies the unsharp mask filter to an image using the ImageFilter.UnsharpMask() class and the Image.filter() method with default values. from PIL import Image, ImageFilter # Open the image original_image = Image.open(”Images/Flower1.jpg”) # Apply the Unsharp Mask filter with default parameter values sharpened_image = original_image.filter(ImageFilter.UnsharpMask()) # Display the original image original_image.show() # Display the sharpened image sharpened_image.show() Output Input image − Output image after applying the unsharp mask filter with default parameter values − Example Here”s another example that applies the Unsharp Mask filter to an image using different parameter values. from PIL import Image, ImageFilter # Open the image using Pillow original_image = Image.open(”Images/Flower1.jpg”) # Apply the Unsharp Mask filter with custom parameters sharpened_image = original_image.filter(ImageFilter.UnsharpMask(radius=4, percent=200, threshold=3)) # Display the original image original_image.show() # Display the sharpened image sharpened_image.show() Output Input image − Output image after applying the unsharp mask filter with custom parameter values − Print Page Previous Next Advertisements ”;

Python Pillow – Enhancing Edges

Python Pillow – Enhancing Edges ”; Previous Next Enhancing edges is a commonly used technique in image processing and computer vision to improve the interpretation and analysis of images. It refers to the process of emphasizing or highlighting the boundaries between different objects or regions in an image. And it aims to improve the visibility of edges, making them more distinct and prominent. Python Pillow library provides the ImageFilter module with a predefined set of filters that can be applied to images using the Image.filter() method. The current version of the library offers two predefined image enhancement filters namely EDGE_ENHANCE and EDGE_ENHANCE_MORE to enhance edges in an image. Enhancing Edges using the ImageFilter.EDGE_ENHANCE Kernel This ImageFilter.EDGE_ENHANCE kernel filter is designed to Enhance the edges in the image. Following are the steps for enhancing the edges in the image − Load the input image using the Image.open() function. Apply the filter() function to the loaded Image object and Provide ImageFilter.EDGE_ENHANCE as an argument to the Image.filter() function, then it returns a PIL.Image.Image object with enhanced edges. Example Here”s an example that demonstrates how to use the ImageFilter.EDGE_ENHANCE filter kernel to enhance edges in an image. from PIL import Image, ImageFilter # Open the input image image = Image.open(”Images/Flower1.jpg”) # Apply edge enhancement filter enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE) # Display the original and enhanced images image.show() enhanced_image.show() Input Image Output Image Output image with enhanced edges − Enhancing Edges using the EDGE_ENHANCE_MORE Kernel This approach works similar to the EDGE_ENHANCE filter, but it applies a stronger enhancement to edges. Example Here”s an example that demonstrates how to use the EDGE_ENHANCE_MORE filter kernel to enhance edges in an image more aggressively. from PIL import Image, ImageFilter # Open the input image image = Image.open(”Images/Flower1.jpg”) # Apply the EDGE_ENHANCE_MORE filter to the image enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE_MORE) # Display the original and enhanced images image.show() enhanced_image.show() Input Image Output Image Output image after applying the EDGE_ENHANCE_MORE filter to the input image − Print Page Previous Next Advertisements ”;

Python Pillow – Adding Filters to an Image

Python Pillow – Adding Filters to an Image ”; Previous Next Image filtering is a fundamental technique for modifying and enhancing images. In image processing, filters are mathematical operations applied to an image to improve its quality, extract specific information, or alter its appearance. They work at the pixel level, applying mathematical operations to pixels within a defined neighborhood, often determined by structuring elements or footprints. These filters are used for a wide range of tasks, including smoothing, sharpening, and enhancing specific image features. They can be implemented through techniques like convolution and frequency domain manipulation. Python Pillow library provides the ImageFilter module with a predefined set of filters that can be applied to images using the Image.filter() method. These filters allow you to change the look and feel of images. The current version of the Python Pillow library offers a range of predefined image enhancement filters. Some of the filters include − BLUR CONTOUR DETAIL EDGE_ENHANCE EDGE_ENHANCE_MORE EMBOSS FIND_EDGES SHARPEN SMOOTH SMOOTH_MORE Applying Filter to an Image By using the Image.filter() method you can apply a specific filter to an image object. This method takes a filter kernel as a parameter and returns an Image object with the applied filter effect. Following is the syntax of the Image.filter() method − Image.filter(filter) This Image.filter() method accept only one parameter which is described below − filter − The filter kernel to be applied. Example Applying the BLUR Filter: Here”s an example of how to blur an input image using the ImageFilter.BLUR filter from the ImageFilter module. from PIL import Image, ImageFilter # Open the input image image = Image.open(”Images/TP logo.jpg”) # Apply the BLUR filter to the image blurred_image = image.filter(filter=ImageFilter.BLUR) # Display the original and blurred images image.show() blurred_image.show() Input Image Output Image Example The following example demonstrates how to apply pillow predefined image enhancement filters to an input image. from PIL import Image, ImageFilter import matplotlib.pyplot as plt # Open the input image image = Image.open(”Images/Flower1.jpg”) # Apply CONTOUR filter to the image image_contour = image.filter(filter=ImageFilter.CONTOUR) # Apply DETAIL filter to the image image_detail = image.filter(filter=ImageFilter.DETAIL) # Apply EDGE_ENHANCE filter to the image image_edge = image.filter(filter=ImageFilter.EDGE_ENHANCE) # Apply EDGE_ENHANCE_MORE filter to the image image_edge_more = image.filter(filter=ImageFilter.EDGE_ENHANCE_MORE) # Apply EMBOSS filter to the image image_emboss = image.filter(filter=ImageFilter.EMBOSS) # Apply FIND_EDGES filter to the image image_edges = image.filter(filter=ImageFilter.FIND_EDGES) # Apply SMOOTH filter to the image image_smooth = image.filter(filter=ImageFilter.SMOOTH) # Apply SMOOTH_MORE filter to the image image_smooth_more = image.filter(filter=ImageFilter.SMOOTH_MORE) # Apply SHARPEN filter to the image image_sharpen = image.filter(filter=ImageFilter.SHARPEN) # Create a subplot for each filtered image and display it using Matplotlib fig, ax = plt.subplots(3, 3, figsize=(12, 10)) # Original Image ax[0, 0].imshow(image) ax[0, 0].set_title(“Original Image”) # CONTOUR filter ax[0, 1].imshow(image_contour) ax[0, 1].set_title(“CONTOUR”) # DETAIL filter ax[0, 2].imshow(image_detail) ax[0, 2].set_title(“DETAIL”) # EDGE_ENHANCE filter ax[1, 0].imshow(image_edge) ax[1, 0].set_title(“EDGE_ENHANCE”) # EDGE_ENHANCE_MORE filter ax[1, 1].imshow(image_edge_more) ax[1, 1].set_title(“EDGE_ENHANCE_MORE”) # EMBOSS filter ax[1, 2].imshow(image_emboss) ax[1, 2].set_title(“EMBOSS”) # FIND_EDGES filter ax[2, 0].imshow(image_edges) ax[2, 0].set_title(“FIND_EDGES”) # SMOOTH filter ax[2, 1].imshow(image_smooth) ax[2, 1].set_title(“SMOOTH”) # SMOOTH_MORE filter ax[2, 2].imshow(image_smooth_more) ax[2, 2].set_title(“SMOOTH_MORE”) # Turn off ax for all subplots for a in ax.flatten(): a.axis(”off”) plt.tight_layout() plt.show() Input Image Output Print Page Previous Next Advertisements ”;

Python Pillow – Embossing Images

Python Pillow – Embossing Images ”; Previous Next In general, embossing used for creating raised relief images on paper or cardstock to achieve a three-dimensional appearance, was notably used by the British postal service during the 19th century to add an elegant touch to stamps. This process involves raising certain parts of the image, design, or text to create a three-dimensional effect. In image processing and computer graphics, image embossing is a technique where each pixel in an image is transformed into either a highlighted or shadowed version, depending on the original image”s light and dark boundaries. Areas with low contrast are replaced by a gray background. The resulting embossed image effectively represents the rate of color change at each location within the original image. Applying an embossing filter to an image can often produce an output resembling a paper or metal-embossed rendition of the original image, hence its name. Python”s Pillow library offers several standard image filters within the ImageFilter module to perform the different filter operations on the image by calling the image.filter() method. In this tutorial, we will see the working of the embossing filter provided by the ImageFilter module. Applying ImageFilter.EMBOSS kernel filter with the Image.filter() method The ImageFilter.EMBOSS filter is one of the built-in filter options available in the current version of the Pillow library and is used to create an embossed effect in the image. Following are the steps for applying image embossing − Load the input image using the Image.open() function. Apply the filter() function to the loaded Image object and provide ImageFilter.EMBOSS as an argument to the function. The function will return the embossed image as a PIL.Image.Image object. Example Here is an example using the Image.filter() method with ImageFilter.EMBOSS kernel filter. from PIL import Image, ImageFilter # Load an input image input_image = Image.open(“Images/TP logo.jpg”) # Apply an emboss effect to the image embossed_result = input_image.filter(ImageFilter.EMBOSS) # Display the input image input_image.show() # Display the modified image with the emboss effect embossed_result.show() Input Image Output Image Output filtered image with emboss effect − Customizing the EMBOSS filter to add depth and azimuth to the embossed image In the source code of the pillow library, we can observe the EMBOSS class in the “ImageFilter.py,” module. This class represents the embossing filter and provides a basic way to create an embossed effect on an image. # Embossing filter. class EMBOSS(BuiltinFilter): name = “Emboss” # fmt: off filterargs = (3, 3), 1, 128, ( -1, 0, 0, 0, 1, 0, 0, 0, 0, ) The filter operates by applying a matrix to the image pixel by pixel. The matrix contains values that determine the transformation applied to each pixel. By modifying the matrix, you can control the azimuth and strength of the embossing effect. The matrix is a 3×3 grid where each element corresponds to the current pixel and its surrounding pixels. The central value represents the current pixel being processed. The filter combines these values based on their weights in the matrix to create the transformed pixel. You can customize the filter by adjusting the scale and offset parameters, which influence the overall strength of the effect. Example Here is an example that demonstrates how to apply a custom emboss filter to an image, allowing you to control the appearance of the emboss effect by adjusting various parameters. from PIL import Image, ImageFilter # Open the input image image = Image.open(”Images/TP logo.jpg”) # modify the filterargs such as scale, offset and the matrix ImageFilter.EMBOSS.filterargs=((3, 3), 2, 150, (0, 0, 0, 0, 1, 0, -2, 0, 0)) # Apply an emboss effect to the image embossed_result = image.filter(ImageFilter.EMBOSS) # Display the input image image.show() # Display the modified image with the emboss effect embossed_result.show() Inputput Image Output Image Output filtered image with custom emboss effect − Print Page Previous Next Advertisements ”;

Python Pillow – Convolution Filters

Python Pillow – Convolution Filters ”; Previous Next In the context of image processing, Convolution involves applying a small matrix (known as convolution kernel) of values to an image. This process results in various filtering effects such as blurring, sharpening, embossing, and edge detection. Each value in the kernel represents a weight or coefficient. This kernel is applied to a corresponding neighborhood of pixels in the image to produce the output pixel value at the corresponding position in the output image. Python”s Pillow library provides a specific class known as “kernel” within its ImageFilter module. This class is used to create convolution kernels of sizes that extend beyond the conventional 5×5 matrices. Creating the Convolution kernel To create a convolution kernel you can use the Kernel() class from the ImageFilter module. It”s important to note that the current version of Pillow supports 3×3 and 5×5 integer and floating-point kernels. And these kernels apply exclusively to images in “L” and “RGB” modes. Following is the syntax of this ImageFilter.Kernel() class − class PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0) Here are the details of the class parameters − size − Represents the kernel size, specified as (width, height). In the current version, the valid sizes are (3,3) or (5,5). kernel − A sequence containing the kernel weights. The kernel is vertically flipped before applying it to the image. scale − Denotes the scale factor. If provided, the result for each pixel is divided by this value. The default value is the sum of the kernel weights. offset − Signifies an offset value. If provided, this value is added to the result after being divided by the scale factor. Example This example demonstrates how to apply a convolution kernel filter to an image using the Image.filter() method. from PIL import Image, ImageFilter # Create an image object original_image = Image.open(”Images/Car_2.jpg”) # Apply the Kernel filter filtered_image = original_image.filter(ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 5, -1, 0, -1, 0))) # Display the original image original_image.show() # Display the filtered image filtered_image.show() Input Image Output Output of the convolution kernel filter − Example Here”s an example of applying a 5×5 emboss convolution kernel filter to an image. from PIL import Image, ImageFilter # Create an image object original_image = Image.open(”Images/Car_2.jpg”) # Define a 5×5 convolution kernel kernel_5x5 = [-2, 0, -1, 0, 0, 0, -2, -1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 1, 2, 0, 0, 0, 1, 0, 2] # Apply the 5×5 convolution kernel filter filtered_image = original_image.filter(ImageFilter.Kernel((5, 5), kernel_5x5, 1, 0)) # Display the original image original_image.show() # Display the filtered image filtered_image.show() Input Image Output Image Output of the convolution kernel filter of size 5X5 − Print Page Previous Next Advertisements ”;

Python Pillow – Identifying Colors

Python Pillow – Identifying Colors ”; Previous Next In the context of image processing and analysis, identifying colors refers to the process of recognizing, categorizing, and extracting information about the different Colors on an Image. It involves the analysis of pixel data to determine the specific colors or color patterns within the image. The Python Pillow library offers valuable tools for this purpose. This tutorial explores two fundamental functions, namely, getcolors() and getdata(), which play a crucial role in efficiently analyzing the color information present within images. Identifying Colors with the getcolors() function The getcolors() function returns a list of colors used within the image, with the colors represented in the image”s mode. For example, in an RGB image, the function returns a tuple containing red, green, and blue color values. For a palette-based (P) image, it returns the index of the color in the palette. The syntax for using this function is − Image.getcolors(maxcolors=256) Where, maxcolors parameter is used to specify the maximum number of colors to retrieve. And it returns an unsorted list of tuples, each containing a count of occurrences and the corresponding color pixel value. If this maxcolors limit is exceeded, the method returns None (default limit: 256 colors). Example This example identifies the most frequent colors in an image using the Image.getcolors() function. It extracts the top 10 most common colors from the image and creates a color palette to visualize and display these colors along with their respective counts. from PIL import Image, ImageDraw # Open an image input_image = Image.open(”Images/colorful-shapes.jpg”) # Get the palette of the top 10 most common colors palette = sorted(input_image.getcolors(maxcolors=100000), reverse=True)[:10] cols = 2 rows = ((len(palette) – 1) // cols) + 1 cellHeight = 70 cellWidth = 200 imgHeight = cellHeight * rows imgWidth = cellWidth * cols result_image = Image.new(“RGB”, (imgWidth, imgHeight), (0, 0, 0)) draw = ImageDraw.Draw(result_image) for idx, (count, color) in enumerate(palette): y0 = cellHeight * (idx // cols) y1 = y0 + cellHeight x0 = cellWidth * (idx % cols) x1 = x0 + (cellWidth // 4) draw.rectangle([x0, y0, x1, y1], fill=color, outline=”black”) draw.text((x1 + 1, y0 + 10), f”Count: {count}”, fill=”white”) # Display the input image input_image.show(”Input Image”) # Display the color chart result_image.show(”Output identified Colors”) Input Image Output identified Colors It is important to note that the getcolors() function returns None when the number of colors in the image is greater than the maxcolors argument. And it primarily works with ”RGB” images, which may not be ideal for all use cases. For scenarios where you need more flexibility or when working with images of different color modes, using the Image.getdata() function in combination with other Python libraries can be a more convenient Identifying Colors with the getdata() function The getdata() function is another function within Pillow Image module that allows users to access the contents of an image as a sequence object containing pixel values. The sequence object is flattened, meaning values for each line follow directly after the values of the zero line, and so on. While the sequence object returned by this method is an internal Pillow data type, it can only support certain sequence operations, such as a Python list, for more convenient manipulation and printing. Here is the syntax of the function − Image.getdata(band=None) The parameters band is used to specify the band to return (default: all bands). To retrieve a single band, provide the index value (e.g., 0 to obtain the “R” band from an “RGB” image). And the function returns a sequence-like object that can be converted into more familiar data structures for further processing. Example Here is an example that identifies the colors in an image and counts the occurrences of each color using the Image.getdata() function. from PIL import Image from collections import defaultdict # Open an imagefrom PIL import Image from collections import defaultdict # Open an image im = Image.open(”Images/sea1.jpg”) # Create a defaultdict to store color counts colors = defaultdict(int) # Iterate through the image pixels and count each color for pixel in im.getdata(): colors[pixel] += 1 # Sort the colors by count in descending order ordered_colors = sorted(colors.items(), key=lambda x: x[1], reverse=True) # Print the top 10 colors and their counts for color, count in ordered_colors[:10]: print(f”Color: {color}, Count: {count}”) from PIL import Image from collections import defaultdict # Open an image im = Image.open(”Images/sea1.jpg”) # Create a defaultdict to store color counts colors = defaultdict(int) # Iterate through the image pixels and count each color for pixel in im.getdata(): colors[pixel] += 1 # Sort the colors by count in descending order ordered_colors = sorted(colors.items(), key=lambda x: x[1], reverse=True) # Print the top 10 colors and their counts for color, count in ordered_colors[:10]: print(f”Color: {color}, Count: {count}”) im = Image.open(”Images/sea1.jpg”) # Create a defaultdict to store color counts colors = defaultdict(int) # Iterate through the image pixels and count each color for pixel in im.getdata(): colors[pixel] += 1 # Sort the colors by count in descending order ordered_colors = sorted(colors.items(), key=lambda x: x[1], reverse=True) # Print the top 10 colors and their counts for color, count in ordered_colors[:10]: print(f”Color: {color}, Count: {count}”) Output Color: (255, 255, 255), Count: 82 Color: (0, 33, 68), Count: 73 Color: (0, 74, 139), Count: 71 Color: (0, 78, 144), Count: 69 Color: (0, 77, 143), Count: 62 Color: (0, 63, 107), Count: 59 Color: (0, 34, 72), Count: 56 Color: (0, 36, 72), Count: 52 Color: (0, 30, 58), Count: 51 Color: (1, 26, 56), Count: 51 Example The following example identifies the most frequent color in an image and counts the occurrences of the color using the Image.getdata() function together with Python collections

Python Pillow – Removing Noise

Python Pillow – Removing Noise ”; Previous Next Removing noise, also referred to as denoising, involves the process of reducing unwanted artifacts in an image. Noise in an image typically appears as random variations in brightness or color that are not part of the original scene or subject being photographed. The goal of image denoising is to enhance the quality of an image by eliminating or reducing these unwanted and distracting artifacts, making the image cleaner, and more visually appealing. The Python pillow library offers a range of denoising filters, allowing users to remove noise from noisy images and recover the original image. In this tutorial, we will explore GaussianBlur and Median filters as effective methods for noise removal. Removing the noise using the GaussianBlur filter Removing the noise from an image using the gaussian blur filter is a widely used technique. This technique is works by applying a convolution filter to the image to smooth out the pixel values. Example Here is an example that uses the ImageFilter.GaussianBlur() filter to remove the noise from an RGB image. from PIL import Image, ImageFilter # Open a Gaussian Noised Image input_image = Image.open(“Images/GaussianNoisedImage.jpg”).convert(”RGB”) # Apply Gaussian blur with some radius blurred_image = input_image.filter(ImageFilter.GaussianBlur(radius=2)) # Display the input and the blurred image input_image.show() blurred_image.show() Input Noised Image Output Noise Removed Image Removing the noise using the Median Filter The median filter is an alternative approach for noise reduction, particularly useful for the images where the noise points are like small and scattered. This function operates by replacing each pixel value with the median value within its local neighborhood. the Pillow library provides the ImageFilter.MedianFilter() filter for this purpose. Example Following example removes the noice from an image using the ImageFilter.MedianFilter(). from PIL import Image, ImageFilter # Open an image input_image = Image.open(“Images/balloons_noisy.png”) # Apply median filter with a kernel size filtered_image = input_image.filter(ImageFilter.MedianFilter(size=3)) # Display the input and the filtered image input_image.show() filtered_image.show() Input Noisy Image Output Median Filtered Image Example This example demonstrates the reduction of salt-and-pepper noise in a grayscale image. This can be done by using a median filter, enhancing the contrast to improve visibility, and subsequently converting the image to binary mode for display. from PIL import Image, ImageEnhance, ImageFilter, ImageOps # Open the image and convert it to grayscale input_image = ImageOps.grayscale(Image.open(”Images/salt-and-pepper noise.jpg”)) # Apply a median filter with a kernel size of 5 to reduce noise filtered_image = input_image.filter(ImageFilter.MedianFilter(5)) # Enhance the contrast of the filtered image contrast_enhancer = ImageEnhance.Contrast(filtered_image) high_contrast_image = contrast_enhancer.enhance(3) # Convert the image to binary binary_image = high_contrast_image.convert(”1”) # Display the input and processed images input_image.show() binary_image.show() Input Noisy Image Output Median Filtered Image Print Page Previous Next Advertisements ”;

Python Pillow – Blur an Image

Python Pillow – Blur an Image ”; Previous Next Blurring an Image is a fundamental concept in image processing, used to reduce the level of detail or sharpness in an image. The primary purpose of blurring is to make an image appear smoother or less sharp. Blurring can be achieved through various mathematical operations, convolution kernels, or filters. Python”s Pillow library offers several standard image filters within the ImageFilter module to perform the different blurring operations on the image by calling the image.filter() method. In this tutorial we will see different image blurring filters provided by the ImageFilter module. Applying Blur Filter to an Image Blurring an image can be done by using the ImageFilter.BLUR filter, which is one of the built-in filter options available in the current version of the Pillow library and is used to create a blurred effect in the image. Following are the steps for applying image blurring − Load the input image using the Image.open() function. Apply the filter() function to the loaded Image object and provide ImageFilter.BLUR as an argument to the function. The function will return the blurred image as a PIL.Image.Image object. Following is the input image used in all the examples of this chapter. Example Here is the example using the Image.filter() method with ImageFilter.BLUR kernel filter. from PIL import Image, ImageFilter # Open an existing image original_image = Image.open(”Images/car_2.jpg”) # Apply a blur filter to the image blurred_image = original_image.filter(ImageFilter.BLUR) # Display the original image original_image.show() # Display the blurred mage blurred_image.show() Output Image Applying the BoxBlur Filter The BoxBlur filter is used to blur an image by setting each pixel”s value to the average value of the pixels within a square box that extends a specified number of pixels in each direction. For this yo can use the BoxBlur() class from the Pillow”s ImageFilter module. Following is the syntax of the ImageFilter.BoxBlur() class − class PIL.ImageFilter.BoxBlur(radius) The PIL.ImageFilter.BoxBlur() class accepts a single parameter − radius − This parameter specifies the size of the square box used for blurring in each direction. It can be provided as either a sequence of two numbers (for the x and y directions) or a single number that applies to both directions. If radius is set to 1, it takes the average of the pixel values within a 3×3 square box (1 pixel in each direction, resulting in 9 pixels in total). A radius value of 0 doesn”t blur the image and returns an identical image. Example Here”s an example that demonstrates how to use the BoxBlur() class for image blurring. from PIL import Image, ImageFilter # Open an existing image original_image = Image.open(”Images/car_2.jpg”) # Apply a Box Blur filter to the image box_blurred_image = original_image.filter(ImageFilter.BoxBlur(radius=3)) # Display the Box Blurred image box_blurred_image.show() Output Image Applying the Gaussian Blur to an Image The GaussianBlur filter is used to blur an image by applying a Gaussian blur, which is a specific type of blur that approximates a Gaussian distribution. This can be done by using the Pillow”s ImageFilter.GaussianBlur() class. Below is the syntax of the ImageFilter.GaussianBlur() class − class PIL.ImageFilter.GaussianBlur(radius=2) The ImageFilter.GaussianBlur() class accepts a single parameter − radius − This parameter specifies the standard deviation of the Gaussian kernel. The standard deviation controls the extent of blurring. You can provide it as either a sequence of two numbers (for the x and y directions) or a single number that applies to both directions. Example Here”s an example that demonstrates how to use the GaussianBlur() class for image blurring. from PIL import Image, ImageFilter # Open an existing image original_image = Image.open(”Images/car_2.jpg”) # Apply a Gaussian Blur filter to the image gaussian_blurred_image = original_image.filter(ImageFilter.GaussianBlur(radius=2)) # Display the Gaussian Blurred image gaussian_blurred_image.show() Output Image Print Page Previous Next Advertisements ”;

Python Pillow – Enhancing Sharpness

Python Pillow – Enhancing Sharpness ”; Previous Next Sharpness is a key factor in defining the clarity and detail in a photograph, and it serves as a valuable tool for emphasizing texture and visual impact. Enhancing Sharpness is an image processing technique used to enhance an image”s edges, details, and contrast, resulting in a clearer and more vivid appearance. This process can significantly enhance the quality and visibility of images that are blurry, noisy, or distorted because of factors such as camera shake, low resolution, or compression. Enhancing Sharpness of an Image In Python Pillow library, enhancing and adjusting the sharpness of an image can be achived by using the ImageEnhance.Sharpness() class. It allows you to adjust the sharpness of an image by applying the enhancement factor. Below is the syntax of the ImageEnhance.Sharpness() class − class PIL.ImageEnhance.Sharpness(image) An enhancement factor represented as a floating-point value, is passed as an argument to the common single interface method, enhance(factor). This factor plays an important in adjusting the image”s sharpness. Using a factor of 0.0 can lead to a completely blurred image, while a factor of 1.0 gives the original image. And higher values enhance the image”s sharpness. Following are the steps to achieve Sharpness Enhancement of an image − Create a Sharpness enhancer object using the ImageEnhance.Sharpness() class. Then apply the enhancement factor to the enhancer object using the enhance() method. Example Here”s an example that demonstrates how to adjust the sharpness of an image by specifying a factor. from PIL import Image, ImageEnhance # Open an image file image = Image.open(”Images/butterfly1.jpg”) # Create a Sharpness object Enhancer = ImageEnhance.Sharpness(image) # Display the original image image.show() # Enhance the sharpness with a factor of 4.0 sharpened_image = enhancer.enhance(4.0) # Display the sharpened image sharpened_image.show() Output The Input image − The output sharpness enhanced image − Example In this example, you can obtain a blurred version of the input image by setting the enhancement factor to a value close to zero. from PIL import Image, ImageEnhance # Open an image file image = Image.open(”Images/butterfly.jpg”) # Create a Sharpness object enhancer = ImageEnhance.Sharpness(image) # Display the original image image.show() # Decrease the sharpness by using a factor of 0.05 sharpened_image = enhancer.enhance(0.05) # Display the sharpened image sharpened_image.show() Output The input image − The output blurred version of the input image − Print Page Previous Next Advertisements ”;

Python Pillow – Enhancing Color

Python Pillow – Enhancing Color ”; Previous Next Enhancing color is an image processing technique that focuses on improving the visual appeal and quality of a digital image by fine-tuning the balance and saturation of its colors. The objective of image color enhancement is to get finer image details while highlighting the useful information. This technique is especially useful in scenarios where images have poor illumination conditions, resulting in darker and low-contrast appearances that require refinement. This technique is widely used in various fields, including graphic design, photography, and digital art, to enhance the visual impact of images. The Python Pillow library (PIL) offers the Color() class within its ImageEnhance module, which enables you to apply color enhancement to images. Enhancing Color of an Image To enhance the color of an image using the Python Pillow library, you can use the ImageEnhance.Color() class, which allows you to adjust the color balance of an image, just as controls on a color TV set. Following is the syntax of the ImageEnhance.Color() class − class PIL.ImageEnhance.Color(image) An enhancement factor represented as a floating-point value, is passed as an argument to the common single interface method, enhance(factor). This factor plays an important role in adjusting the color balance. A factor of 0.0 will give a black and white image. A factor of 1.0 gives the original image. Following are the steps to achieve color enhancement of an image − Create a color enhancer object using the ImageEnhance.Color() class. Then apply the enhancement factor to the enhancer object using the enhance() method. Example Here is an example that enhances the color of an image with a factor of 3.0. from PIL import Image, ImageEnhance # Open an image file image = Image.open(”Images/Tajmahal_2.jpg”) # Create a Color object color_enhancer = ImageEnhance.Color(image) # Display the original image image.show() # Enhance the color with a factor of 3.0 colorful_image = color_enhancer.enhance(3.0) # Display the enhanced color image colorful_image.show() Output Input image − Output highly color enhanced image − Example Here is an example that enhances the color of an image with a lower enhancement factor(0.0). from PIL import Image, ImageEnhance # Open an image file image = Image.open(”Images/Tajmahal_2.jpg”) # Create a Color object color_enhancer = ImageEnhance.Color(image) # Display the original image image.show() # Enhance the color with a factor of 0.0 colorful_image = color_enhancer.enhance(0.0) # Display the enhanced color image colorful_image.show() Output Input image − Output color enhanced image with a factor of 0.0 − Print Page Previous Next Advertisements ”;