python 实现图片特效处理


Posted in Python onApril 03, 2022

前言:

对于 ​图片处理​,在日常生活中我们常常能够看到。

比如发个朋友圈之前,我们需要给自己的​照片加个滤镜​;在上传头像时候,需要​对照片进行裁剪​,这些都是图片的处理。

待处理的原图:

python 实现图片特效处理

一、黑白特效

  • 将图片处理后,变为黑白颜色
  • 把像素的R,G,B三个通道数值都置为:​​r*0.299+g*0.587+b*0.114​
  • 效果

黑白特效:

python 实现图片特效处理

代码:

 #!/usr/bin/env python
# encoding: utf-8

import numpy as np
from PIL import Image

class picture:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
self.path = 'assets/picture.jpeg'

def hello(self):
'''
This is a welcome speech
:return: self
'''
print('*' * 50)
print(' ' * 20 + '图片转换特效之黑白')
print(' ' * 5 + '作者: autofelix Date: 2022-01-17 13:14')
print('*' * 50)
return self

def run(self):
'''
The program entry
'''
im = self.to_black_white()
im.show()
im.save('assets/black_white.jpeg')

def to_black_white(self):
'''
Picture to black white
'''
im = np.asarray(Image.open(self.path).convert('RGB'))
trans = np.array([[0.299, 0.587, 0.114], [0.299, 0.587, 0.114], [0.299, 0.587, 0.114]]).transpose()
im = np.dot(im, trans)
return Image.fromarray(np.array(im).astype('uint8'))

if __name__ == '__main__':
picture().hello().run()

二、流年特效

  • 将图片处理后,变为流年特效
  • 把R通道的数值开平方,然后乘以一个参数
  • 效果

流年特效:

python 实现图片特效处理

代码:

#!/usr/bin/env python
# encoding: utf-8

import numpy as np
from PIL import Image

class picture:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
self.path = 'assets/picture.jpeg'

def hello(self):
'''
This is a welcome speech
:return: self
'''
print('*' * 50)
print(' ' * 20 + '图片转换特效之流年')
print(' ' * 5 + '作者: autofelix Date: 2022-01-17 13:14')
print('*' * 50)
return self

def run(self):
'''
The program entry
'''
im = self.fleeting()
im.show()
im.save('assets/fleeting.jpeg')

def fleeting(self, params=12):
'''
Picture to fleeting
'''
im = np.asarray(Image.open(self.path).convert('RGB'))
im1 = np.sqrt(im * [1.0, 0.0, 0.0]) * params
im2 = im * [0.0, 1.0, 1.0]
im = im1 + im2
return Image.fromarray(np.array(im).astype('uint8'))

if __name__ == '__main__':
picture().hello().run()

三、旧电影特效

  • 将图片处理后,变为旧电影特效
  • 把像素的R,G,B三个通道数值,3个通道的分别乘以3个参数后求和,最后把超过255的值置为255
  • 效果

旧电影特效:

python 实现图片特效处理

代码:

#!/usr/bin/env python
# encoding: utf-8

import numpy as np
from PIL import Image

class picture:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
self.path = 'assets/picture.jpeg'

def hello(self):
'''
This is a welcome speech
:return: self
'''
print('*' * 50)
print(' ' * 20 + '图片转换特效之旧电影')
print(' ' * 5 + '作者: autofelix Date: 2022-01-17 13:14')
print('*' * 50)
return self

def run(self):
'''
The program entry
'''
im = self.old_film()
im.show()
im.save('assets/old_film.jpeg')

def old_film(self):
'''
Picture to old film
'''
im = np.asarray(Image.open(self.path).convert('RGB'))
trans = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]).transpose()
im = np.dot(im, trans).clip(max=255)
return Image.fromarray(np.array(im).astype('uint8'))

if __name__ == '__main__':
picture().hello().run()

四、反色特效

  • 将图片处理后,变为反色特效
  • 这个最简单了,用255减去每个通道的原来的数值
  • 效果

反色特效:

python 实现图片特效处理

代码:

#!/usr/bin/env python
# encoding: utf-8

import numpy as np
from PIL import Image

class picture:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
self.path = 'assets/picture.jpeg'

def hello(self):
'''
This is a welcome speech
:return: self
'''
print('*' * 50)
print(' ' * 20 + '图片转换特效之反色')
print(' ' * 5 + '作者: autofelix Date: 2022-01-17 13:14')
print('*' * 50)
return self

def run(self):
'''
The program entry
'''
im = self.reverse()
im.show()
im.save('assets/reverse.jpeg')

def reverse(self):
'''
Picture to reverse
'''
im = 255 - np.asarray(Image.open(self.path).convert('RGB'))
return Image.fromarray(np.array(im).astype('uint8'))

if __name__ == '__main__':
picture().hello().run()

到此这篇关于python 实现图片特效处理的文章就介绍到这了,更多相关python 图片特效内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python跳出循环语句continue与break的区别
Aug 25 Python
python关闭windows进程的方法
Apr 18 Python
Python爬虫代理IP池实现方法
Jan 05 Python
python 信息同时输出到控制台与文件的实例讲解
May 11 Python
解决python3.5 正常安装 却不能直接使用Tkinter包的问题
Feb 22 Python
python for和else语句趣谈
Jul 02 Python
python从list列表中选出一个数和其对应的坐标方法
Jul 20 Python
python产生模拟数据faker库的使用详解
Nov 04 Python
matplotlib实现数据实时刷新的示例代码
Jan 05 Python
教你怎么用Python实现多路径迷宫
Apr 29 Python
使用pandas或numpy处理数据中的空值(np.isnan()/pd.isnull())
May 14 Python
Python 全局空间和局部空间
Apr 06 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
解析Extjs与php数据交互(增删查改)
2013/06/25 PHP
PHP CURL获取cookies模拟登录的方法
2013/11/04 PHP
php上传图片到指定位置路径保存到数据库的具体实现
2013/12/30 PHP
thinkPHP5框架auth权限控制类与用法示例
2018/06/12 PHP
php提供实现反射的方法和实例代码
2019/09/17 PHP
asp.net和asp下ACCESS的参数化查询
2008/06/11 Javascript
Ajax搜索结果页面下方的分页按钮的生成
2012/04/05 Javascript
js匿名函数的调用示例(形式多种多样)
2014/08/20 Javascript
一个JavaScript函数把URL参数解析成Json对象
2014/09/24 Javascript
浅谈EasyUI中Treegrid节点的删除
2015/03/01 Javascript
javascript实现在网页任意处点左键弹出隐藏菜单的方法
2015/05/13 Javascript
jQuery+HTML5实现手机摇一摇换衣特效
2015/06/05 Javascript
jQuery拖动元素并对元素进行重新排序
2015/12/30 Javascript
JS代码实现百度地图 画圆 删除标注
2016/10/12 Javascript
js中的触发事件对象event.srcElement与event.target详解
2017/03/15 Javascript
Angular组件化管理实现方法分析
2017/03/17 Javascript
原生JS实现ajax与ajax的跨域请求实例
2017/12/01 Javascript
优雅地使用loading(推荐)
2019/04/20 Javascript
jquery实现商品sku多属性选择功能(商品详情页)
2019/12/20 jQuery
微信h5静默和非静默授权获取用户openId的方法和步骤
2020/06/08 Javascript
利用JS判断元素是否为数组的方法示例
2021/01/08 Javascript
[01:46]辉夜杯—打造中国DOTA新格局
2015/12/25 DOTA
python实现读取命令行参数的方法
2015/05/22 Python
Python语言生成水仙花数代码示例
2017/12/18 Python
浅析Windows 嵌入python解释器的过程
2019/07/26 Python
Python之指数与E记法的区别详解
2019/11/21 Python
Python使用Socket实现简单聊天程序
2020/02/28 Python
Python使用itcaht库实现微信自动收发消息功能
2020/07/13 Python
python批量修改文件名的示例
2020/09/27 Python
我的画教学反思
2014/04/28 职场文书
班子四风对照检查材料思想汇报
2014/09/29 职场文书
教师批评与自我批评心得体会
2014/10/16 职场文书
党风廉正建设责任书
2015/01/29 职场文书
财务人员个人工作总结
2015/02/27 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
python入门学习关于for else的特殊特性讲解
2021/11/20 Python