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列表推导式的使用方法
Nov 21 Python
python实现人人网登录示例分享
Jan 19 Python
在Django中创建第一个静态视图
Jul 15 Python
Python 的内置字符串方法小结
Mar 15 Python
最近Python有点火? 给你7个学习它的理由!
Jun 26 Python
Python简单实现阿拉伯数字和罗马数字的互相转换功能示例
Apr 17 Python
Python中if elif else及缩进的使用简述
May 31 Python
在python中只选取列表中某一纵列的方法
Nov 28 Python
使用Python控制摄像头拍照并发邮件
Apr 23 Python
在Django中实现添加user到group并查看
Nov 18 Python
Python多进程multiprocessing、进程池用法实例分析
Mar 24 Python
基于Python的EasyGUI学习实践
May 07 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
PHP下操作Linux消息队列完成进程间通信的方法
2010/07/24 PHP
三个类概括PHP的五种设计模式
2012/09/05 PHP
php判断linux下程序问题实例
2015/07/09 PHP
Mac系统下安装PHP Xdebug
2018/03/30 PHP
JQuery 图片滚动轮播示例代码
2014/03/24 Javascript
JavaScript中的Math 使用介绍
2014/04/21 Javascript
Backbone.js的Hello World程序实例
2015/06/19 Javascript
JS实现将Asp.Net的DateTime Json类型转换为标准时间的方法
2016/08/02 Javascript
js轮播图无缝滚动效果
2017/06/17 Javascript
js + css实现标签内容切换功能(实例讲解)
2017/10/09 Javascript
vue 自定义组件 v-model双向绑定、 父子组件同步通信的多种写法
2017/11/27 Javascript
Angular 4.x+Ionic3踩坑之Ionic 3.x界面传值详解
2018/03/13 Javascript
小程序实现列表点赞功能
2018/11/02 Javascript
layui table动态表头 改变表格头部 重新加载表格的方法
2019/09/21 Javascript
Python批量修改文件后缀的方法
2014/01/26 Python
Python isinstance函数介绍
2015/04/14 Python
解密Python中的描述符(descriptor)
2015/06/03 Python
Python工程师面试题 与Python基础语法相关
2016/01/14 Python
Python随机数用法实例详解【基于random模块】
2017/04/18 Python
解决pycharm remote deployment 配置的问题
2019/06/27 Python
python实现的分析并统计nginx日志数据功能示例
2019/12/21 Python
解决django接口无法通过ip进行访问的问题
2020/03/27 Python
Python Django中的STATIC_URL 设置和使用方式
2020/03/27 Python
通过代码简单了解django model序列化作用
2020/11/12 Python
python 基于opencv 绘制图像轮廓
2020/12/11 Python
香港草莓网:Strawberrynet香港
2019/05/10 全球购物
经典c++面试题四
2015/05/14 面试题
什么是命名空间(NameSpace)
2015/11/24 面试题
如何整合JQuery和Prototype
2014/01/31 面试题
八一建军节活动方案
2014/02/10 职场文书
幼儿园小班植树节活动方案
2014/03/04 职场文书
计划生育目标责任书
2015/05/09 职场文书
小学语文国培研修日志
2015/11/13 职场文书
幼儿园2016年感恩节活动总结
2016/04/01 职场文书
go:垃圾回收GC触发条件详解
2021/04/24 Golang
详解TypeScript中的类型保护
2021/04/29 Javascript