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文件和目录操作函数小结
Jul 11 Python
编写Python脚本来实现最简单的FTP下载的教程
May 04 Python
python去除所有html标签的方法
May 05 Python
分析Python中设计模式之Decorator装饰器模式的要点
Mar 02 Python
Python中线程的MQ消息队列实现以及消息队列的优点解析
Jun 29 Python
浅谈python对象数据的读写权限
Sep 12 Python
DataFrame中的object转换成float的方法
Apr 10 Python
python+selenium打印当前页面的titl和url方法
Jun 22 Python
便捷提取python导入包的属性方法
Oct 15 Python
python爬取cnvd漏洞库信息的实例
Feb 14 Python
Python实现疫情通定时自动填写功能(附代码)
May 27 Python
Python中的turtle画箭头,矩形,五角星
Mar 16 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
十天学会php(2)
2006/10/09 PHP
php下通过POST还是GET来传值
2008/06/05 PHP
PHP 之 写时复制介绍(Copy On Write)
2014/05/13 PHP
php车辆违章查询数据示例
2016/10/14 PHP
JQuery 遮罩层实现(mask)实现代码
2010/01/09 Javascript
使用jQuery实现dropdownlist的联动效果(sharepoint 2007)
2011/03/30 Javascript
Ext JS 4官方文档之三 -- 类体系概述与实践
2012/12/16 Javascript
Jquery实现的tab效果可以指定默认显示第几页
2013/10/16 Javascript
通过pjax实现无刷新翻页(兼容新版jquery)
2014/01/31 Javascript
使用HTML+CSS+JS制作简单的网页菜单界面
2015/07/27 Javascript
jQuery简单实现两级下拉菜单效果代码
2015/09/15 Javascript
JS+CSS实现闪烁字体效果代码
2016/04/05 Javascript
JS+CSS3实现超炫的散列画廊特效
2016/07/16 Javascript
JavaScript调试的多个必备小Tips
2017/01/15 Javascript
js绑定事件和解绑事件
2017/04/27 Javascript
node-sass安装失败的原因与解决方法
2017/09/04 Javascript
详解React Native顶|底部导航使用小技巧
2017/09/14 Javascript
import与export在node.js中的使用详解
2017/09/28 Javascript
node的process以及child_process模块学习笔记
2018/03/06 Javascript
JavaScript基础之静态方法和实例方法分析
2018/12/26 Javascript
JS实现的全选、全不选及反选功能【案例】
2019/02/19 Javascript
如何在JavaScript中优雅的提取循环内数据详解
2019/03/04 Javascript
VSCode 配置uni-app的方法
2020/07/11 Javascript
python实现基于两张图片生成圆角图标效果的方法
2015/03/26 Python
python中字符串的操作方法大全
2018/06/03 Python
浅谈Python2之汉字编码为unicode的问题(即类似\xc3\xa4)
2019/08/12 Python
如何实现在jupyter notebook中播放视频(不停地展示图片)
2020/04/23 Python
Python基于内置函数type创建新类型
2020/10/22 Python
索尼巴西商店:Sony巴西
2019/06/21 全球购物
大学校园生活自我鉴定
2014/01/13 职场文书
创先争优活动方案
2014/02/12 职场文书
《北京的春节》教学反思
2014/04/07 职场文书
观看《信仰》心得体会
2016/01/15 职场文书
2016年少先队活动总结
2016/04/06 职场文书
阿里云日志过滤器配置日志服务
2022/04/09 Servers
Tomcat配置访问日志和线程数
2022/05/06 Servers