Skip to main content

ImageEnhancingUsingPython

Image Enhancement Using Python


Image enhancement is a process of improving the visual quality of an image, making it more visually appealing, easier to interpret, or better suited for a specific application. In Python, you can perform image enhancement using a combination of libraries and techniques to adjust various aspects of an image. Here is a general description of image enhancement using Python:


1. Image Loading and Preprocessing:

   The first step in image enhancement is to load the image into Python. Libraries such as Pillow or OpenCV can be used for this purpose. Once the image is loaded, you can perform preprocessing tasks such as resizing, cropping, or converting to grayscale.

2. Brightness and Contrast Adjustment:

   Adjusting the brightness and contrast of an image can significantly enhance its appearance. Python libraries like Pillow allow you to increase or decrease these parameters to improve the overall visibility of the image.


3. Histogram Equalization:

   Histogram equalization is a technique used to improve the contrast of an image by redistributing the intensity values. Python offers functions for histogram equalization, especially with libraries like OpenCV.


4. Color Correction:

   Color correction can be vital for images, especially when dealing with photographs. You can use techniques to adjust color balance, saturation, and color temperature to make the image look more natural or bring out specific colors.


5. Sharpening and Blurring:

   Image sharpness can be enhanced by using sharpening filters or techniques. On the other hand, blurring can be used to reduce noise or enhance certain stylistic effects. Libraries like Pillow and OpenCV provide functions for applying sharpening and blurring filters.


6. Noise Reduction:

   Removing noise from an image is crucial for improving image quality. Python offers various methods to reduce noise, including median filtering and Gaussian filtering.


7. Resolution Enhancement:

   Upscaling the resolution of an image, such as converting it to a higher resolution (e.g., 8K), can be done using resampling techniques provided by libraries like Pillow.


8. Image Enhancement with Machine Learning:

   Advanced image enhancement can be achieved using machine learning models. Deep learning models, such as convolutional neural networks (CNNs), can be used to enhance image quality by learning features from a dataset and applying them to the input image.


9. Post-processing and Visualization:

   After applying enhancement techniques, it's essential to visualize the results and fine-tune the settings to achieve the desired outcome. You can save the enhanced image or display it using Python's visualization libraries.


10. Automation and Batch Processing:

   Python allows for automating the image enhancement process for multiple images in a batch. This is especially useful when working with large datasets.


Image enhancement in Python is a versatile and powerful process, enabling you to improve the visual quality of images for various applications, including photography, computer vision, medical imaging, and more. The choice of enhancement techniques and libraries depends on the specific requirements of the task at hand, making Python a valuable tool for working with digital images.

GitHub Link:

You can download or clone projects:https://github.com/Sivatech24/ImageEnhancingUsingPython.git

Input Image:

The image used was downloaded form pexels website

Photo details

Uploaded on August 26th, 2016

Dimensions:2200 x 1414

Aspect Ratio:1100:707

File Size:1.27 MB

DPIAdjustment code:

from PIL import Image


# Open the image

image = Image.open('input.jpg')


# Set the desired DPI (e.g., 300 DPI)

dpi_value = (300, 300)


# Set the DPI in the image info

image.info['dpi'] = dpi_value


# Save the image with the new DPI

image.save('dpi_adjusted_image.jpg')

Output:


Image Resolution Changer code:
from PIL import Image

# Open the input image
input_image = Image.open('input.jpg')

# Define the desired width and height
width = input('Enter the width')  # Replace with your desired width
desired_width = int(width) 
height = input('Enter the height')  # Replace with your desired height
desired_height = int(height)

# Upscale the image to the desired resolution
upscaled_image = input_image.resize((desired_width, desired_height), Image.NEAREST)

# Save the upscaled image
upscaled_image.save('upscaled_image.jpg')

# Close the input image
input_image.close()
Output:


Sharpening code:
from PIL import Image, ImageFilter

# Open the input image
input_image = Image.open('input.jpg')

# Apply sharpening filter
sharpened_image = input_image.filter(ImageFilter.SHARPEN)

# Save the sharpened image
sharpened_image.save('sharpened_image.jpg')

# Close the input image
input_image.close()
Output:



Using Deep Learning for Image Upscaling code:
import cv2
import torch
import numpy as np
from esrgan_pytorch import ESRGAN

# Load the ESRGAN model
model = ESRGAN()

# Load the input image
image = cv2.imread('input_image.jpg')

# Convert BGR to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Upscale the image using ESRGAN
upscaled_image = model.upscale(image_rgb)

# Convert the output image to BGR
upscaled_image_bgr = cv2.cvtColor(upscaled_image, cv2.COLOR_RGB2BGR)

# Save the upscaled image
cv2.imwrite('output_image.jpg', upscaled_image_bgr)

Using OpenCV for Basic Image Enhancement and Upscaling code:
import cv2

# Load the image
image = cv2.imread('input.jpg')

# Perform image enhancement (e.g., adjust brightness and contrast)
enhanced_image = cv2.convertScaleAbs(image, alpha=2.0, beta=50)

# Upscale the image
upscaled_image = cv2.resize(enhanced_image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)

# Save the enhanced and upscaled image
cv2.imwrite('output_image.jpg', upscaled_image)

Thank You For Visiting Our Blog.

Comments

Popular posts from this blog

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11  Welcome to this step-by-step Stable Diffusion WebUI 1.10.1 installation guide! In this tutorial, we will walk you through the complete setup process on Windows 11 , including downloading and installing Git , setting up Python 3.10.6 , cloning the AUTOMATIC1111 repository , and configuring .gitignore for a clean and efficient installation. By following this guide, you’ll be able to generate AI-generated images using Stable Diffusion with ease. Whether you're new to AI image generation or an experienced user, this guide ensures that your setup is optimized for performance and stability. 🔗 Required Downloads: Before we begin, make sure to download the following tools: ✅ Git for Windows – Download Here ✅ Stable Diffusion WebUI (AUTOMATIC1111) – Download Here ✅ Python 3.10.6 – Download Here 🛠️ Step-by-Step Installation Process 1️⃣ Install Git for Windows Git is required to clone the ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...