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使用百度API上传文件到百度网盘代码分享
Nov 08 Python
Python 中开发pattern的string模板(template) 实例详解
Apr 01 Python
python3处理含有中文的url方法
May 10 Python
python使用selenium登录QQ邮箱(附带滑动解锁)
Jan 23 Python
对pandas通过索引提取dataframe的行方法详解
Feb 01 Python
详解Python打包分发工具setuptools
Aug 05 Python
python实现引用其他路径包里面的模块
Mar 09 Python
Python批量处理csv并保存过程解析
May 16 Python
BeautifulSoup获取指定class样式的div的实现
Dec 07 Python
用pushplus+python监控亚马逊到货动态推送微信
Jan 29 Python
Python3使用Selenium获取session和token方法详解
Feb 16 Python
Python语言规范之Pylint的详细用法
Jun 24 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
Php连接及读取和写入mysql数据库的常用代码
2014/08/11 PHP
ThinkPHP模板输出display用法分析
2014/11/26 PHP
PHP的引用详解
2015/02/22 PHP
浅谈PHP中的数据传输CURL
2016/09/06 PHP
php利用递归实现删除文件目录的方法
2016/09/23 PHP
Laravel5.1 框架Middleware中间件基本用法实例分析
2020/01/04 PHP
脚本吧 - 幻宇工作室用到js,超强推荐base.js
2006/12/23 Javascript
学习YUI.Ext 第六天--关于树TreePanel(Part 2异步获取节点)
2007/03/10 Javascript
JavaScript事件处理器中的event参数使用介绍
2013/05/24 Javascript
JavaScript全排列的六种算法 具体实现
2013/06/29 Javascript
node.js中的fs.readlink方法使用说明
2014/12/17 Javascript
Bootstrap框架实现广告轮播效果
2016/11/28 Javascript
清除输入框内的空格
2016/12/21 Javascript
详解Vue微信公众号开发踩坑全记录
2017/08/21 Javascript
angular4 如何在全局设置路由跳转动画的方法
2017/08/30 Javascript
Vue2(三)实现子菜单展开收缩,带动画效果实现方法
2019/04/28 Javascript
vue实现动态按钮功能
2019/05/13 Javascript
vue和H5 draggable实现拖拽并替换效果
2020/07/29 Javascript
Python中str is not callable问题详解及解决办法
2017/02/10 Python
Python操作MongoDB详解及实例
2017/05/18 Python
Python中列表list以及list与数组array的相互转换实现方法
2017/09/22 Python
R语言 vs Python对比:数据分析哪家强?
2017/11/17 Python
Python系统监控模块psutil功能与经典用法分析
2018/05/24 Python
python write无法写入文件的解决方法
2019/01/23 Python
Python中的相关分析correlation analysis的实现
2019/08/29 Python
Mac 使用python3的matplot画图不显示的解决
2019/11/23 Python
解决keras GAN训练是loss不发生变化,accuracy一直为0.5的问题
2020/07/02 Python
浅谈django不使用restframework自定义接口与使用的区别
2020/07/15 Python
在IE6系列等老式浏览器中使用HTML5的新标签实现方案
2012/12/25 HTML / CSS
华为c/c++笔试题
2016/01/25 面试题
实习生单位鉴定意见
2013/12/04 职场文书
环保建议书300字
2014/05/14 职场文书
关心下一代工作先进事迹
2014/08/15 职场文书
2015年挂职干部工作总结
2015/05/14 职场文书
闪闪的红星观后感
2015/06/08 职场文书
家电创业计划书
2019/08/05 职场文书