Files
AiDA_Python/app/service/generate_image/utils/adjust_contrast.py
2024-04-23 20:45:34 +08:00

31 lines
1017 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cv2
def adjust_contrast(image, alpha=1.5, beta=-60):
"""
调整图像的对比度和亮度。
参数:
image_path (numpy): 图像的路径。
alpha (float): 控制对比度的系数。alpha > 1 增加对比度alpha < 1 减少对比度。
beta (int): 用于调整亮度的值,可以是正或负。
返回:
adjusted_image (ndarray): 调整对比度后的图像。
"""
adjusted_image = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
return adjusted_image
# 使用示例
if __name__ == "__main__":
image = cv2.imread('output_6.png') # 替换为你的图片路径
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
alpha = 1.5 # 对比度系数大于1增加对比度
beta = -60 # 亮度调整这里设置为0不改变亮度
# 调整图像对比度
result_image = adjust_contrast(image, alpha, beta)
# 可以选择保存调整后的图像
cv2.imwrite('adjusted_image.jpg', result_image) # 保存调整后的图片