欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 调整图片亮度的示例

程序员文章站 2022-04-27 11:05:26
实现效果实现代码import matplotlib.pyplot as pltfrom skimage import iofile_name='d:/2020121173119242.png'img=...

实现效果

python 调整图片亮度的示例

实现代码

import matplotlib.pyplot as plt
from skimage import io

file_name='d:/2020121173119242.png'
img=io.imread(file_name)

increment = -10.0

img = img * 1.0 
i = (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])/3.0 + 0.001

mask_1 = i > 128.0

r = img [:, :, 0]
g = img [:, :, 1]
b = img [:, :, 2]

rhs = (r*128.0 - (i - 128.0) * 256.0) / (256.0 - i) 
ghs = (g*128.0 - (i - 128.0) * 256.0) / (256.0 - i)
bhs = (b*128.0 - (i - 128.0) * 256.0) / (256.0 - i)

rhs = rhs * mask_1 + (r * 128.0 / i) * (1 - mask_1)
ghs = ghs * mask_1 + (g * 128.0 / i) * (1 - mask_1)
bhs = bhs * mask_1 + (b * 128.0 / i) * (1 - mask_1)

i_new = i + increment - 128.0

mask_2 = i_new > 0.0

r_new = rhs + (256.0-rhs) * i_new / 128.0
g_new = ghs + (256.0-ghs) * i_new / 128.0
b_new = bhs + (256.0-bhs) * i_new / 128.0

r_new = r_new * mask_2 + (rhs + rhs * i_new/128.0) * (1-mask_2)
g_new = g_new * mask_2 + (ghs + ghs * i_new/128.0) * (1-mask_2)
b_new = b_new * mask_2 + (bhs + bhs * i_new/128.0) * (1-mask_2)

img_out = img * 1.0

img_out[:, :, 0] = r_new
img_out[:, :, 1] = g_new
img_out[:, :, 2] = b_new

img_out = img_out/255.0

# 饱和处理
mask_1 = img_out < 0 
mask_2 = img_out > 1

img_out = img_out * (1-mask_1)
img_out = img_out * (1-mask_2) + mask_2

plt.figure()
plt.imshow(img/255.0)
plt.axis('off')

plt.figure(2)
plt.imshow(img_out)
plt.axis('off')

plt.figure(3)
plt.imshow(i/255.0, plt.cm.gray)
plt.axis('off')

plt.show()

以上就是python 调整图片亮度的示例的详细内容,更多关于python 调整图片亮度的资料请关注其它相关文章!