溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果示例

發(fā)布時間:2020-10-18 15:04:24 來源:腳本之家 閱讀:153 作者:Matrix_11 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果。分享給大家供大家參考,具體如下:

這里用Python 實(shí)現(xiàn) PS 里的圖像調(diào)整–黑白,PS 里的黑白并不是簡單粗暴的將圖像轉(zhuǎn)為灰度圖,而是做了非常精細(xì)的處理,具體的算法原理和效果圖可以參考附錄說明。

比起之前的程序,對代碼進(jìn)行了優(yōu)化,完全用矩陣運(yùn)算代替了 for 循環(huán),運(yùn)算效率提升了很多。具體的代碼如下:

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 圖像調(diào)整算法——黑白

黑白調(diào)整

Photoshop CS的圖像黑白調(diào)整功能,是通過對紅、黃、綠、青、藍(lán)和洋紅等6種顏色的比例調(diào)節(jié)來完成的。能更精細(xì)地將彩色圖片轉(zhuǎn)換為高質(zhì)量的黑白照片。

Photoshop CS圖像黑白調(diào)整功能的計算公式為:

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兩種分量顏色所形成的復(fù)色比率。

默認(rèn)的單色及復(fù)色比率為:

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:

%%%%% 程序?qū)崿F(xiàn)圖像的黑白調(diào)整功能
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;
%%%% 各種顏色的默認(rèn)比率
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運(yùn)行結(jié)果如下:

原圖:

Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果示例

運(yùn)行效果圖:

Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果示例

更多關(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è)計有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI