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实现多行注释的另类方法
Aug 22 Python
TensorFlow中权重的随机初始化的方法
Feb 11 Python
Python利用ORM控制MongoDB(MongoEngine)的步骤全纪录
Sep 13 Python
解决python "No module named pip" 的问题
Oct 13 Python
关于不懂Chromedriver如何配置环境变量问题解决方法
Jun 12 Python
python django生成迁移文件的实例
Aug 31 Python
通过实例简单了解Python中yield的作用
Dec 11 Python
django多种支付、并发订单处理实例代码
Dec 13 Python
如何基于Django实现上下文章跳转
Sep 16 Python
Python机器学习之底层实现KNN
Jun 20 Python
Python3.8官网文档之类的基础语法阅读
Sep 04 Python
python机器学习Github已达8.9Kstars模型解释器LIME
Nov 23 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
站长助手-网站web在线管理程序 v1.0 下载
2007/05/12 PHP
PHP连接和操作MySQL数据库基础教程
2014/09/29 PHP
jQuery 事件队列调整方法
2009/09/18 Javascript
使用apply方法处理数组的三个技巧[译]
2012/09/20 Javascript
Bootstrap表单布局样式代码
2016/05/31 Javascript
vue.js 表格分页ajax 异步加载数据
2016/10/18 Javascript
JS中正则表达式全局匹配模式 /g用法详解
2017/04/01 Javascript
基于jquery实现多级菜单效果
2017/07/25 jQuery
Angular4学习之Angular CLI的安装与使用教程
2018/01/04 Javascript
详解react-refetch的使用小例子
2019/02/15 Javascript
Layui 带多选框表格监听事件以及按钮自动点击写法实例
2019/09/02 Javascript
Openlayers学习之加载鹰眼控件
2020/09/28 Javascript
python概率计算器实例分析
2015/03/25 Python
python实现域名系统(DNS)正向查询的方法
2016/04/19 Python
Python中将dataframe转换为字典的实例
2018/04/13 Python
Python检查ping终端的方法
2019/01/26 Python
Python基于mysql实现学生管理系统
2019/02/21 Python
Django+Xadmin构建项目的方法步骤
2019/03/06 Python
python使用BeautifulSoup与正则表达式爬取时光网不同地区top100电影并对比
2019/04/15 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
2019/09/10 Python
pyqt5数据库使用详细教程(打包解决方案)
2020/03/25 Python
Django DRF路由与扩展功能的实现
2020/06/03 Python
python中的垃圾回收(GC)机制
2020/09/21 Python
用纯css3和html制作泡沫对话框实现代码
2013/03/21 HTML / CSS
Canvas制作旋转的太极的示例
2018/03/09 HTML / CSS
html5写一个BUI折叠菜单插件的实现方法
2019/09/11 HTML / CSS
瑞典手机壳品牌:Richmond & Finch
2018/04/28 全球购物
幼儿园毕业家长感言
2014/02/10 职场文书
写求职信要注意什么问题
2014/04/12 职场文书
十周年庆典策划方案
2014/06/03 职场文书
国际语言毕业生求职信
2014/07/08 职场文书
国际政治学专业推荐信
2014/09/26 职场文书
出纳工作检讨书
2014/10/18 职场文书
MySQL表类型 存储引擎 的选择
2021/11/11 MySQL
如何利用golang运用mysql数据库
2022/03/13 Golang
Android开发之底部导航栏的快速实现
2022/04/28 Java/Android