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

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

WBOY
WBOYOriginal
2023-08-17 21:24:264975browse

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')

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)

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:
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