python 调整灰度图像对比度_Python实现PS图像调整之对比度调整功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)PS圖像調(diào)整之對比度調(diào)整功能。分享給大家供大家參考,具體如下:
這里用 Python 實(shí)現(xiàn) PS 里的圖像調(diào)整–對比度調(diào)整。具體的算法原理如下:
(1)、nRGB = RGB + (RGB - Threshold) * Contrast / 255
公式中,nRGB表示圖像像素新的R、G、B分量,RGB表示圖像像素R、G、B分量,Threshold為給定的閾值,Contrast為處理過的對比度增量。
Photoshop對于對比度增量,是按給定值的正負(fù)分別處理的:
當(dāng)增量等于-255時(shí),是圖像對比度的下端極限,此時(shí),圖像RGB各分量都等于閾值,圖像呈全灰色,灰度圖上只有1條線,即閾值灰度;
當(dāng)增量大于-255且小于0時(shí),直接用上面的公式計(jì)算圖像像素各分量;
當(dāng)增量等于255時(shí),是圖像對比度的上端極限,實(shí)際等于設(shè)置圖像閾值,圖像由最多八種顏色組成,灰度圖上最多8條線,即紅、黃、綠、青、藍(lán)、紫及黑與白;
當(dāng)增量大于0且小于255時(shí),則先按下面公式(2)處理增量,然后再按上面公式(1)計(jì)算對比度:
(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()
運(yùn)行效果圖
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
本文標(biāo)題: Python實(shí)現(xiàn)PS圖像調(diào)整之對比度調(diào)整功能示例
本文地址: http://www.cppcns.com/jiaoben/python/218683.html
總結(jié)
以上是生活随笔為你收集整理的python 调整灰度图像对比度_Python实现PS图像调整之对比度调整功能示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 35岁前要培养的66种思维(上)
- 下一篇: python3 Flask 多人答题(完