Adjust color curves in python similar to gimp

When working with images in Python, it is often necessary to adjust the color curves to enhance the overall appearance. This can be done in various ways, depending on the specific requirements and the tools available. In this article, we will explore three different approaches to adjust color curves in Python, similar to GIMP.

Approach 1: Using the PIL library

The Python Imaging Library (PIL) provides a wide range of image processing capabilities, including color curve adjustments. To adjust color curves using PIL, we can follow these steps:

from PIL import Image

def adjust_color_curves(image):
    # Convert the image to RGB mode if it is not already
    image = image.convert("RGB")

    # Get the pixel data of the image
    pixels = image.load()

    # Iterate over each pixel and adjust the color curves
    for i in range(image.width):
        for j in range(image.height):
            r, g, b = pixels[i, j]

            # Adjust the color curves here
            # ...

            pixels[i, j] = (r, g, b)

    return image

# Load the image
image = Image.open("input.jpg")

# Adjust the color curves
adjusted_image = adjust_color_curves(image)

# Save the adjusted image
adjusted_image.save("output.jpg")

This approach uses the PIL library to load the image, convert it to RGB mode if necessary, and then iterate over each pixel to adjust the color curves. The specific adjustments can be made within the inner loop, where the RGB values of each pixel are accessed and modified accordingly. Finally, the adjusted image is saved to a file.

Approach 2: Using the OpenCV library

Another popular library for image processing in Python is OpenCV. It provides a comprehensive set of functions for various image manipulation tasks, including color curve adjustments. Here’s how we can adjust color curves using OpenCV:

import cv2

def adjust_color_curves(image):
    # Convert the image to the LAB color space
    lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)

    # Split the LAB image into L, A, and B channels
    l_channel, a_channel, b_channel = cv2.split(lab_image)

    # Adjust the color curves on the L channel
    # ...

    # Merge the adjusted L channel with the A and B channels
    adjusted_lab_image = cv2.merge((l_channel, a_channel, b_channel))

    # Convert the LAB image back to the BGR color space
    adjusted_image = cv2.cvtColor(adjusted_lab_image, cv2.COLOR_LAB2BGR)

    return adjusted_image

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

# Adjust the color curves
adjusted_image = adjust_color_curves(image)

# Save the adjusted image
cv2.imwrite("output.jpg", adjusted_image)

In this approach, we use the OpenCV library to load the image, convert it to the LAB color space, and split it into L, A, and B channels. The color curve adjustments are then applied to the L channel. After that, the adjusted L channel is merged with the A and B channels, and the image is converted back to the BGR color space. Finally, the adjusted image is saved to a file.

Approach 3: Using the scikit-image library

The scikit-image library is another powerful tool for image processing in Python. It provides a wide range of functions for various image manipulation tasks, including color curve adjustments. Here’s how we can adjust color curves using scikit-image:

from skimage import io, exposure

def adjust_color_curves(image):
    # Adjust the color curves using the exposure module
    adjusted_image = exposure.adjust_gamma(image, gamma=1.5)

    return adjusted_image

# Load the image
image = io.imread("input.jpg")

# Adjust the color curves
adjusted_image = adjust_color_curves(image)

# Save the adjusted image
io.imsave("output.jpg", adjusted_image)

In this approach, we use the scikit-image library to load the image and adjust the color curves using the exposure module. The specific adjustments can be made by calling the appropriate function from the exposure module. Finally, the adjusted image is saved to a file.

After exploring these three approaches, it is difficult to determine which one is better as it depends on the specific requirements and the tools available. The PIL library provides a straightforward way to adjust color curves, while OpenCV and scikit-image offer more advanced functionalities for image processing. It is recommended to choose the approach that best suits your needs and familiarity with the libraries.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents