Python: PS 图像调整--对比度调整

本文用 Python 实现 PS 里的图像调整–对比度调整。具体的算法原理如下:
(1)、nRGB = RGB + (RGB - Threshold) * Contrast / 255
公式中,nRGB表示图像像素新的R、G、B分量,RGB表示图像像素R、G、B分量,Threshold为给定的阈值,Contrast为处理过的对比度增量。
Photoshop对于对比度增量,是按给定值的正负分别处理的:
当增量等于-255时,是图像对比度的下端极限,此时,图像RGB各分量都等于阈值,图像呈全灰色,灰度图上只有1条线,即阈值灰度;
当增量大于-255且小于0时,直接用上面的公式计算图像像素各分量;
当增量等于255时,是图像对比度的上端极限,实际等于设置图像阈值,图像由最多八种颜色组成,灰度图上最多8条线,即红、黄、绿、青、蓝、紫及黑与白;
当增量大于0且小于255时,则先按下面公式(2)处理增量,然后再按上面公式(1)计算对比度:
(2)、nContrast = 255 * 255 / (255 - Contrast) - 255
公式中的nContrast为处理后的对比度增量,Contrast为给定的对比度增量。

import matplotlib.pyplot as plt
from skimage import io


file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name)

img = img * 1.0

thre = img.mean()

# -100 - 100
contrast = -55.0

img_out = img * 1.0

if contrast <= -255.0:
    img_out = (img_out >= 0) + thre -1
elif contrast > -255.0 and contrast < 0:
    img_out = img + (img - thre) * contrast / 255.0   
elif contrast < 255.0 and contrast > 0:    
    new_con = 255.0 *255.0 / (256.0-contrast) - 255.0
    img_out = img + (img - thre) * new_con / 255.0   
else:
    mask_1 = img > thre 
    img_out = mask_1 * 255.0

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

效果图:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值