发布于 2018-02-14 00:25:34 | 303 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python3 Cookbook中文版,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


这篇文章主要介绍了Python实现PS图像调整之对比度调整功能,结合实例形式分析了Python实现PS图像对比度调整的原理、实现方法及相关操作技巧,需要的朋友可以参考下

本文实例讲述了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.phperz.com')
plt.imshow(img/255.0)
plt.axis('off')
plt.figure(2)
plt.title('www.phperz.com')
plt.imshow(img_out)
plt.axis('off')
plt.show()

运行效果图

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务