Python: PS 图像调整--颜色梯度

来源:互联网 发布:plsql连接其他数据库 编辑:程序博客网 时间:2024/06/05 08:03

本文用 Python 实现 PS 中的色彩图,可以看到颜色的各种渐变,具体的效果可以参考以前的博客:

http://blog.csdn.net/matrix_space/article/details/46906849

和之前的程序相比,这里利用矩阵的运算替代了 for 循环,提升了运行的效率。

import numpy as npimport matplotlib.pyplot as pltfrom skimage import ioimport numpy.matlibfrom skimage import img_as_floatfile_name='D:/Visual Effects/PS Algorithm/4.jpg';img=io.imread(file_name)img = img_as_float(img)row, col, channel = img.shaperNW = 0.5rNE = 1.0     rSW = 1.0rSE = 0.0     gNW = 0.0gNE = 0.5gSW = 0.0gSE = 1.0bNW = 1.0bNE = 0.0bSW = 1.0bSE = 0.0xx = np.arange (col) yy = np.arange (row)x_mask = numpy.matlib.repmat (xx, row, 1)y_mask = numpy.matlib.repmat (yy, col, 1)y_mask = np.transpose(y_mask)fx = x_mask * 1.0 / colfy = y_mask * 1.0 / rowp = rNW + (rNE - rNW) * fxq = rSW + (rSE - rSW) * fxr = ( p + (q - p) * fy )r[r<0] = 0r[r>1] =1p = gNW + (gNE - gNW) * fxq = gSW + (gSE - gSW) * fxg = ( p + (q - p) * fy )g[g<0] = 0g[g>1] =1p = bNW + (bNE - bNW) * fxq = bSW + (bSE - bSW) * fxb = ( p + (q - p) * fy )b[b<0] = 0.0b[b>1] = 1.0img[:, :, 0] = rimg[:, :, 1] = gimg[:, :, 2] = bplt.figure(1)plt.imshow(img)plt.axis('off');plt.show();