Home Backend Development Python Tutorial How to adjust the brightness and contrast of an image using Python

How to adjust the brightness and contrast of an image using Python

Aug 17, 2023 pm 09:24 PM
Python adjusts picture brightness Python adjusts picture contrast

How to adjust the brightness and contrast of an image using Python

How to use Python to adjust the brightness and contrast of pictures

Introduction:
In image processing, adjusting brightness and contrast is one of the common and important operations. Python provides a wealth of image processing libraries, such as PIL, OpenCV, etc., which can easily implement these two operations. This article will introduce how to use Python code to adjust the brightness and contrast of images and give corresponding code examples.

Part 1: Adjust image brightness
The adjustment of image brightness can be achieved by changing the RGB value of each pixel. The specific method is to add a fixed increment to the RGB value of each pixel. The following is a sample code that uses the PIL library to adjust the brightness of an image:

from PIL import ImageEnhance

# 打开图像
image = Image.open('image.jpg')

# 创建Enhancer对象
enhancer = ImageEnhance.Brightness(image)

# 调整亮度为原来的2倍
bright_image = enhancer.enhance(2)

# 显示图像
bright_image.show()

# 保存图像
bright_image.save('bright_image.jpg')
Copy after login

In the above code, first open an image through the Image.open() function, and then use the ImageEnhance.Brightness() function to create an Enhancer object. Then use the enhance() method of the Enhancer object to adjust the brightness of the image. The parameter represents the multiple of the brightness. Finally, call the show() method to display the adjusted image, or you can use the save() method to save the image.

Part 2: Adjusting Image Contrast
The adjustment of image contrast is achieved by changing the relative brightness of each pixel. The specific method is to multiply the RGB value of each pixel by a fixed increment. The following is a sample code that uses the OpenCV library to adjust the contrast of an image:

import cv2

# 读取图像
image = cv2.imread('image.jpg')

# 提取亮度通道
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
v_channel = hsv_image[:,:,2]

# 调整亮度为原来的2倍
contrast_image = cv2.addWeighted(v_channel, 2, np.zeros(v_channel.shape, v_channel.dtype), 0, 0)

# 将亮度通道替换回原图像
hsv_image[:,:,2] = contrast_image
contrast_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)

# 显示图像
cv2.imshow('contrast_image', contrast_image)
cv2.waitKey(0)

# 保存图像
cv2.imwrite('contrast_image.jpg', contrast_image)
Copy after login

In the above code, an image is first read using the cv2.imread() function, and then the image is converted using the cv2.cvtColor() function. Convert from BGR color space to HSV color space because HSV color space is more sensitive to changes in brightness. Then by extracting the brightness channel hsv_image[:,:,2], a two-dimensional array v_channel representing brightness is obtained.

Then use the cv2.addWeighted() function to linearly combine the brightness channels to adjust the contrast, where the first parameter represents the input image, and the second parameter represents the multiple of brightness, here set to 2. Finally, the adjusted brightness channel is replaced back to the original image, and the image is converted from the HSV color space back to the BGR color space through the cv2.cvtColor() function.

Finally, the adjusted image is displayed through the cv2.imshow() function, cv2.waitKey(0) is used to wait for the key, and the cv2.imwrite() function is used to save the image.

Conclusion:
Through the above code examples, we can see that using Python to adjust the brightness and contrast of an image is very simple and can be achieved with just a few lines of code. Through different parameter settings, different degrees of brightness and contrast adjustment can be achieved to meet different needs. In practical applications, appropriate adjustment methods and parameters can be selected according to specific situations to achieve the best results. I hope this article will be helpful to readers in their learning and practice in image processing.

The above is the detailed content of How to adjust the brightness and contrast of an image 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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles