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的random模块吗?
Dec 12 Python
在pandas多重索引multiIndex中选定指定索引的行方法
Nov 16 Python
对Python 两大环境管理神器 pyenv 和 virtualenv详解
Dec 31 Python
Python基于matplotlib画箱体图检验异常值操作示例【附xls数据文件下载】
Jan 07 Python
Django model update的多种用法介绍
Mar 28 Python
python实现静态服务器
Sep 05 Python
关于Python Tkinter Button控件command传参问题的解决方式
Mar 04 Python
Python面向对象魔法方法和单例模块代码实例
Mar 25 Python
python 瀑布线指标编写实例
Jun 03 Python
Python私有属性私有方法应用实例解析
Sep 15 Python
python 爬虫如何正确的使用cookie
Oct 27 Python
教你怎么用Python实现多路径迷宫
Apr 29 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
PHP文件注释标记及规范小结
2012/04/01 PHP
PHP中判断变量为空的几种方法分享
2013/08/26 PHP
ajax返回值中有回车换行、空格的解决方法分享
2013/10/24 PHP
PHP程序漏洞产生的原因分析与防范方法说明
2014/03/06 PHP
php生成唯一数字id的方法汇总
2015/11/18 PHP
Laravel解决nesting level错误和隐藏index.php的问题
2019/10/12 PHP
动态加载外部javascript文件的函数代码分享
2011/07/28 Javascript
jQuery 快速结束当前正在执行的动画
2013/11/20 Javascript
JS实现多物体缓冲运动实例代码
2013/11/29 Javascript
javascript获取当前鼠标坐标的方法
2015/01/10 Javascript
jquery利用命名空间移除绑定事件的方法
2015/03/11 Javascript
jquery让指定的元素闪烁显示的方法
2015/03/17 Javascript
利用JS实现页面删除并重新排序功能
2016/12/09 Javascript
jQuery EasyUI之验证框validatebox实例详解
2017/04/10 jQuery
BootStrap Table实现server分页序号连续显示功能(当前页从上一页的结束序号开始)
2017/09/12 Javascript
no-vnc和node.js实现web远程桌面的完整步骤
2019/08/11 Javascript
微信小程序拼接图片链接无底洞深入探究
2019/09/03 Javascript
Python中使用第三方库xlrd来读取Excel示例
2015/04/05 Python
Python 基础教程之str和repr的详解
2017/08/20 Python
python 将列表中的字符串连接成一个长路径的方法
2018/10/23 Python
python urllib爬虫模块使用解析
2019/09/05 Python
python datetime处理时间小结
2020/04/16 Python
python openssl模块安装及用法
2020/12/06 Python
HTML5 SEO优化的一些建议
2020/08/27 HTML / CSS
英国在线玫瑰专家:InterRose
2019/12/01 全球购物
百度JavaScript笔试题
2015/01/15 面试题
护士专业推荐信
2013/11/02 职场文书
集中整治工作方案
2014/05/01 职场文书
“四风”查摆问题自我剖析材料
2014/09/27 职场文书
教育合作协议范本
2014/10/17 职场文书
2014年销售部工作总结
2014/12/01 职场文书
2014年路政工作总结
2014/12/10 职场文书
电影开国大典观后感
2015/06/04 职场文书
《围炉夜话》110句人生箴言,精辟有内涵,引人深思
2019/10/23 职场文书
python实现图片九宫格分割的示例
2021/04/25 Python
css弧边选项卡的项目实践
2023/05/07 HTML / CSS