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


Posted in Python onJanuary 25, 2018

本文实例讲述了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实现PS图像调整颜色梯度效果示例

运行效果:

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

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

Python 相关文章推荐
Python中用Descriptor实现类级属性(Property)详解
Sep 18 Python
Python实现设置windows桌面壁纸代码分享
Mar 28 Python
python获取一组数据里最大值max函数用法实例
May 26 Python
windows下python之mysqldb模块安装方法
Sep 07 Python
基于Python代码编辑器的选用(详解)
Sep 13 Python
pycharm下打开、执行并调试scrapy爬虫程序的方法
Nov 29 Python
对Python3中bytes和HexStr之间的转换详解
Dec 04 Python
解决python 读取 log日志的编码问题
Dec 24 Python
Python实现点云投影到平面显示
Jan 18 Python
详解用 python-docx 创建浮动图片
Jan 24 Python
Python干货实战之八音符酱小游戏全过程详解
Oct 24 Python
python创建字典及相关管理操作
Apr 13 Python
Python使用requests发送POST请求实例代码
Jan 25 #Python
python使用pandas实现数据分割实例代码
Jan 25 #Python
python实现闹钟定时播放音乐功能
Jan 25 #Python
python实现壁纸批量下载代码实例
Jan 25 #Python
python命令行解析之parse_known_args()函数和parse_args()使用区别介绍
Jan 24 #Python
Python动刷新抢12306火车票的代码(附源码)
Jan 24 #Python
python中的set实现不重复的排序原理
Jan 24 #Python
You might like
简单示例AJAX结合PHP代码实现登录效果代码
2008/07/25 PHP
php获取文件内容最后一行示例
2014/01/09 PHP
在Debian系统下配置LNMP的教程
2015/07/09 PHP
laravel 去掉index.php伪静态的操作方法
2019/10/12 PHP
JS在textarea光标处插入文本的小例子
2013/03/22 Javascript
js 限制input只能输入数字、字母和汉字等等
2013/12/18 Javascript
JavaScript事件委托的技术原理探讨示例
2014/04/17 Javascript
JavaScript数组函数unshift、shift、pop、push使用实例
2014/08/27 Javascript
谷歌showModalDialog()方法不兼容出现对话窗口的解决办法
2016/02/15 Javascript
prototype.js常用函数详解
2016/06/18 Javascript
完美实现js选项卡切换效果(二)
2017/03/08 Javascript
详解webpack2+node+react+babel实现热加载(hmr)
2017/08/24 Javascript
微信小程序tabBar模板用法实例分析【附demo源码下载】
2017/11/28 Javascript
vue路由导航守卫和请求拦截以及基于node的token认证的方法
2019/04/07 Javascript
在antd4.0中Form使用initialValue操作
2020/11/02 Javascript
[57:36]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第三场 2月1日
2021/03/11 DOTA
Python读写unicode文件的方法
2015/07/10 Python
Python3 操作符重载方法示例
2017/11/23 Python
Python绘制3d螺旋曲线图实例代码
2017/12/20 Python
python实现定时自动备份文件到其他主机的实例代码
2018/02/23 Python
python根据文本生成词云图代码实例
2019/11/15 Python
解决python DataFrame 打印结果不换行问题
2020/04/09 Python
解决jupyter运行pyqt代码内核重启的问题
2020/04/16 Python
巴西网上药房:onofre
2016/11/21 全球购物
高校学生干部的自我评价分享
2013/11/04 职场文书
研发工程师岗位职责
2014/04/28 职场文书
我的职业生涯规划:打造自己的运动帝国
2014/09/18 职场文书
九寨沟导游词
2015/02/02 职场文书
辞职信格式模板
2015/02/27 职场文书
政工师工作总结2015
2015/05/26 职场文书
观看安全警示教育片心得体会
2016/01/15 职场文书
在 Golang 中实现 Cache::remember 方法详解
2021/03/30 Python
详解thinkphp的Auth类认证
2021/05/28 PHP
基于Python实现对比Exce的工具
2022/04/07 Python
IDEA 2022 Translation 未知错误 翻译文档失败
2022/04/24 Java/Android
windows系统安装配置nginx环境
2022/06/28 Servers