Home Backend Development Python Tutorial How to color match images using Python

How to color match images using Python

Aug 19, 2023 pm 02:10 PM
python picture color matching

How to color match images using Python

How to use Python to color match images

Introduction:
In modern society, image processing has been widely used in many fields, such as movie special effects, medicine Image diagnosis, etc. Among them, image color matching is an important technology, which can make the colors between different pictures consistent, thereby improving user experience. This article will introduce how to use Python to color match images, and explain it in detail through code examples.

1. Install dependent libraries

Before we begin, we need to ensure that the Python environment has been installed and the PIL library (Python Imaging Library) has been installed. If the PIL library is not installed, you can install it through the following command:

pip install pillow
Copy after login

2. Read the image data

First, we need to read the data of the image to be matched and the reference image, and add Convert it into a data structure that can be manipulated. Suppose we have two pictures: image.jpg is the picture to be matched, reference.jpg is the reference picture, the code example is as follows:

from PIL import Image

def read_image(filename):
    image = Image.open(filename)
    data = list(image.getdata())
    width, height = image.size
    return data, width, height

image_data, image_width, image_height = read_image('image.jpg')
reference_data, reference_width, reference_height = read_image('reference.jpg')
Copy after login

3. Calculate each The average and standard deviation of each channel

In order to achieve color matching, we need to calculate the average and standard deviation of each channel of the image to be matched and the reference image. The code example is as follows:

import numpy as np

def calculate_mean_std(data):
    pixels = np.array(data, dtype=np.float32)
    mean = np.mean(pixels, axis=0)
    std = np.std(pixels, axis=0)
    return mean, std

image_mean, image_std = calculate_mean_std(image_data)
reference_mean, reference_std = calculate_mean_std(reference_data)
Copy after login

4. Color matching

With the mean and standard deviation of each channel, we can use the following formula for color matching:

matched_data = (image_data - image_mean) / image_std * reference_std + reference_mean
Copy after login

The code example is as follows:

def match_color(data, mean, std, reference_mean, reference_std):
    matched_data = np.array(data, dtype=np.float32)
    matched_data = (matched_data - mean) / std * reference_std + reference_mean
    matched_data = matched_data.clip(0, 255)
    return list(matched_data.astype(np.uint8))

matched_image_data = match_color(image_data, image_mean, image_std, reference_mean, reference_std)
Copy after login

5. Save the matched image

Finally, we save the matched image data as a new image file. The code example is as follows:

def save_image(data, width, height, filename):
    image = Image.new('RGB', (width, height))
    image.putdata(data)
    image.save(filename)

save_image(matched_image_data, image_width, image_height, 'matched_image.jpg')
Copy after login

Conclusion:
Through the above steps, we have learned how to use Python to color match images. This technology has wide applications in image processing, design and other fields, and can effectively improve the quality and consistency of images. I hope this article is helpful to you, and you are welcome to try it and apply it to actual projects.

The above is the detailed content of How to color match images using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...

See all articles