python黑的调白的

您所在的位置:网站首页 ps黑白颜色对调怎么调的 python黑的调白的

python黑的调白的

2024-07-15 20:23| 来源: 网络整理| 查看: 265

本文实例讲述了Python实现PS图像调整黑白效果。分享给大家供大家参考,具体如下:

这里用Python 实现 PS 里的图像调整–黑白,PS 里的黑白并不是简单粗暴的将图像转为灰度图,而是做了非常精细的处理,具体的算法原理和效果图可以参考附录说明。

比起之前的程序,对代码进行了优化,完全用矩阵运算代替了 for 循环,运算效率提升了很多。具体的代码如下:

import numpy as np

import matplotlib.pyplot as plt

from skimage import io

file_name='D:/Image Processing/PS Algorithm/4.jpg';

img=io.imread(file_name)

img = img * 1.0

Color_ratio = np.zeros(6)

Color_ratio[0]=0.4; # Red

Color_ratio[1]=0.6; # Yellow

Color_ratio[2]=0.4; # Green

Color_ratio[3]=0.6; # Cyan

Color_ratio[4]=0.2; # Blue

Color_ratio[5]=0.8; # Magenta

max_val = img.max(axis = 2)

min_val = img.min(axis = 2)

sum_val = img.sum(axis = 2)

mid_val = sum_val - max_val - min_val

mask_r = (img[:, :, 0] - min_val - 0.01) > 0

mask_r = 1 - mask_r

mask_g = (img[:, :, 1] - min_val - 0.01) > 0

mask_g = 1 - mask_g

mask_b = (img[:, :, 2] - min_val - 0.01) > 0

mask_b = 1 - mask_b

ratio_max_mid = mask_r * Color_ratio[3] + mask_g * Color_ratio[5] + mask_b * Color_ratio[1]

mask_r = (img[:, :, 0] - max_val + 0.01) < 0

mask_r = 1 - mask_r

mask_g = (img[:, :, 1] - max_val + 0.01) < 0

mask_g = 1 - mask_g

mask_b = (img[:, :, 2] - max_val + 0.01) < 0

mask_b = 1 - mask_b

ratio_max= mask_r * Color_ratio[4] + mask_g * Color_ratio[0] + mask_b * Color_ratio[2]

I_out = max_val * 1.0

I_out = (max_val-mid_val)*ratio_max + (mid_val-min_val)*ratio_max_mid + min_val

plt.figure()

plt.imshow(img/255.0)

plt.axis('off')

plt.figure(2)

plt.imshow(I_out/255.0, plt.cm.gray)

plt.axis('off')

plt.show()

附录:PS 图像调整算法——黑白

黑白调整

Photoshop CS的图像黑白调整功能,是通过对红、黄、绿、青、蓝和洋红等6种颜色的比例调节来完成的。能更精细地将彩色图片转换为高质量的黑白照片。

Photoshop CS图像黑白调整功能的计算公式为:

gray= (max - mid) * ratio_max + (mid - min) * ratio_max_mid + min

公式中:gray为像素灰度值,max、mid和min分别为图像像素R、G、B分量颜色的最大值、中间值和最小值,ratio_max为max所代表的分量颜色(单色)比率,ratio_max_mid则为max与mid两种分量颜色所形成的复色比率。

默认的单色及复色比率为:

Color_Ratio(1)=0.4;     %%%% Red

Color_Ratio(2)=0.6;     %%%% Yellow

Color_Ratio(3)=0.4;     %%%% Green

Color_Ratio(4)=0.6;     %%%% Cyan

Color_Ratio(5)=0.2;     %%%% Blue

Color_Ratio(6)=0.8;     %%%% Magenta

Program:

%%%%% 程序实现图像的黑白调整功能

clc;

clear all;

close all;

Image=imread('9.jpg');

Image=double(Image);

R=Image(:,:,1);

G=Image(:,:,2);

B=Image(:,:,3);

[row, col] = size(R);

Gray_img(1:row,1:col)=0;

Sum_rgb=R+G+B;

%%%% 各种颜色的默认比率

Color_Ratio(1:6)=0;

Color_Ratio(1)=0.4; %%%% Red

Color_Ratio(2)=0.6; %%%% Yellow

Color_Ratio(3)=0.4; %%%% Green

Color_Ratio(4)=0.6; %%%% Cyan

Color_Ratio(5)=0.2; %%%% Blue

Color_Ratio(6)=0.8; %%%% Magenta

for i=1:row

for j=1:col

r=R(i,j);

g=G(i,j);

b=B(i,j);

Max_value=max(r,max(g,b));

Min_value=min(r,min(g,b));

Mid_value=Sum_rgb(i,j)-Max_value-Min_value;

if(Min_value==r)

Index=0;

elseif(Min_value==g)

Index=2;

else

Index=4;

end

ratio_max_mid=Color_Ratio(mod(Index+3,6)+1);

if(Max_value==r)

Index=1;

elseif(Max_value==g)

Index=3;

else

Index=5;

end

ratio_max=Color_Ratio(Index);

Temp=(Max_value-Mid_value)*ratio_max+(Mid_value-Min_value)...

*ratio_max_mid+Min_value;

Gray_img(i,j)=(Max_value-Mid_value)*ratio_max+(Mid_value-Min_value)...

*ratio_max_mid+Min_value;

end

end

imshow(Image/255);

figure, imshow(Gray_img/255);

本例Python运行结果如下:

原图:

运行效果图:

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

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

本文标题: Python实现PS图像调整黑白效果示例

本文地址: http://www.cppcns.com/jiaoben/python/218573.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3