Python实现PS图像调整颜色梯度效果示例

yipeiwu_com5年前Python基础

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

这里用 Python 实现 PS 中的色彩图,可以看到颜色的各种渐变,具体的效果可以参考附录说明

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

import numpy as np
import matplotlib.pyplot as plt
from skimage import io
import numpy.matlib
from skimage import img_as_float
file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name)
img = img_as_float(img)
row, col, channel = img.shape
rNW = 0.5
rNE = 1.0
rSW = 1.0
rSE = 0.0
gNW = 0.0
gNE = 0.5
gSW = 0.0
gSE = 1.0
bNW = 1.0
bNE = 0.0
bSW = 1.0
bSE = 0.0
xx = 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 / col
fy = y_mask * 1.0 / row
p = rNW + (rNE - rNW) * fx
q = rSW + (rSE - rSW) * fx
r = ( p + (q - p) * fy )
r[r<0] = 0
r[r>1] =1
p = gNW + (gNE - gNW) * fx
q = gSW + (gSE - gSW) * fx
g = ( p + (q - p) * fy )
g[g<0] = 0
g[g>1] =1
p = bNW + (bNE - bNW) * fx
q = bSW + (bSE - bSW) * fx
b = ( p + (q - p) * fy )
b[b<0] = 0.0
b[b>1] = 1.0
img[:, :, 0] = r
img[:, :, 1] = g
img[:, :, 2] = b
plt.figure(1)
plt.imshow(img)
plt.axis('off');
plt.show();

附录:PS 色调— —颜色梯度

  clc;
  clear all;
  close all;
  addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
  I=imread('4.jpg');
  Image=double(I)/255;
  [height, width, depth]=size(Image);
  rNW=1.0;   gNW=0.0;  bNW=0.0;
  rNE=1.0;   gNE=1.0;  bNE=0.0;
  rSW=0.0;   gSW=0;   bSW=1.0;
  rSE=0.0;   gSE=1.0;  bSE=0.0;
  Img_new=Image;
  for ii=1:height
    for jj=1:width
      fx = jj / width;
      fy = ii / height;
      p = rNW + (rNE - rNW) * fx;
      q = rSW + (rSE - rSW) * fx;
      r = ( p + (q - p) * fy );
      r = min(max(r, 0), 1);
      p = gNW + (gNE - gNW) * fx;
      q = gSW + (gSE - gSW) * fx;
      g = ( p + (q - p) * fy );
      g = min(max(g, 0) ,1);
      p = bNW + (bNE - bNW) * fx;
      q = bSW + (bSE - bSW) * fx;
      b = ( p + (q - p) * fy );
      b = min(max(b, 0), 1);
      Img_new(ii, jj, 1)=r;
      Img_new(ii, jj, 2)=g;
      Img_new(ii, jj, 3)=b;
    end  
  end
  imshow(Img_new);
  imwrite(Img_new, 'out.jpg');

参考来源:http://www.jhlabs.com/index.html

本例Python运行效果图:

原图:

运行效果:

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

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

相关文章

python发送邮件接收邮件示例分享

接收邮件 复制代码 代码如下:import poplib,pdb,email,re,timefrom email import header POP_ADDR = r'pop.126.c...

Python数据可视化:饼状图的实例讲解

Python数据可视化:饼状图的实例讲解

使用python实现论文里面的饼状图: 原图: python代码实现: # # 饼状图 # plot.figure(figsize=(8,8)) labels = [u'Ca...

Python中字符串的处理技巧分享

一、如何拆分含有多种分隔符的字符串? 实际案例 我们要把某个字符串依据分隔符号拆分不同的字符段,该字符串包含多种不同的分隔符,例如: s = 'asd;aad|dasd|dasd,...

Pyqt实现无边框窗口拖动以及窗口大小改变

本文实例为大家分享了Pyqt实现无边框窗口拖动及大小改变的具体代码,供大家参考,具体内容如下 做个记录,绘制边框阴影可以忽略这里不是主要 根据网上某位仁兄Qt的实现转过来的大笑,上完整代...

对python3 sort sorted 函数的应用详解

python3 sorted取消了对cmp的支持。 python3 帮助文档: sorted(iterable,key=None,reverse=False) key接受一个函数,...