Python实现 PS 图像调整中的亮度调整

yipeiwu_com5年前Python基础

本文用 Python 实现 PS 图像调整中的亮度调整,具体的算法原理和效果可以参考之前的博客:

/post/164191.htm

import matplotlib.pyplot as plt
from skimage import io
file_name='D:/Image Processing/PS Algorithm/4.jpg';
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实现 PS 图像调整中的亮度调整 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Python入门_条件控制(详解)

Python入门_条件控制(详解)

条件控制其实就是if...else...(如果...条件是成立的,就做...;反之,就做...)的使用,其基本结构是: 具体看下面这个例子: def account_login()...

python基于http下载视频或音频

一、简介 这里介绍使用python基于http下载视频或音频。 二、关键点 1、断点续传 视频或音频文件一般比较大,所以通过需要断点续传。方式通过在http的header里添加Range...

使用python进行广告点击率的预测的实现

使用python进行广告点击率的预测的实现

当前在线广告服务中,广告的点击率(CTR)是评估广告效果的一个非常重要的指标。 因此,点击率预测系统是必不可少的,并广泛用于赞助搜索和实时出价。那么如何计算广告的点击率呢? 广告的点击率...

Python如何import文件夹下的文件(实现方法)

Python如何import文件夹下的文件(实现方法)

Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行...

NumPy统计函数的实现方法

numpy.amin()和numpy.amax()numpy.amin()用于计算数组中元素沿着指定轴的最小值。numpy.amax()用于计算数组中元素沿着指定轴的最大值a=np.arr...