Python实现PS图像调整之对比度调整功能示例

yipeiwu_com7年前Python基础

本文实例讲述了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为给定的对比度增量。

# -*- coding: utf-8 -*-
#! python3
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.title('www.jb51.net')
plt.imshow(img/255.0)
plt.axis('off')
plt.figure(2)
plt.title('www.jb51.net')
plt.imshow(img_out)
plt.axis('off')
plt.show()

运行效果图

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python绘制已知点的坐标的直线实例

如下所示: import matplotlib.pyplot as plt import numpy as np x = [11422,11360,11312,112...

Python的iOS自动化打包实例代码

Python的iOS自动化打包实例代码

前言 这段时间刚刚学习了一段时间的Python,加上自己是做iOS开发的,就想着用Python来做一个自动化打包,可以自动完成打包,上传到蒲公英,并且发送邮箱给测试人员. 一是可以减...

对python中的os.getpid()和os.fork()函数详解

如下所示: import os import sys import time processNmae = 'parent' print "Program executing...

python机器学习理论与实战(二)决策树

python机器学习理论与实战(二)决策树

        决策树也是有监督机器学习方法。 电影《无耻混蛋》里有一幕游戏,在德军小酒馆里有几个人在玩20问题游戏...

详解python和matlab的优势与区别

Python是一种面向对象的解释型计算机程序设计语言。Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议...