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 client使用http post 到server端的代码
Feb 10 Python
python使用PythonMagick将jpg图片转换成ico图片的方法
Mar 26 Python
Python线程池模块ThreadPoolExecutor用法分析
Dec 28 Python
Django模型序列化返回自然主键值示例代码
Jun 12 Python
python 类的继承 实例方法.静态方法.类方法的代码解析
Aug 23 Python
Python 格式化输出_String Formatting_控制小数点位数的实例详解
Feb 04 Python
Python图像处理库PIL的ImageEnhance模块使用介绍
Feb 26 Python
解决Django提交表单报错:CSRF token missing or incorrect的问题
Mar 13 Python
python使用建议技巧分享(三)
Aug 18 Python
Pycharm Git 设置方法
Sep 15 Python
pycharm 复制代码出现空格的解决方式
Jan 15 Python
JAVA SpringMVC实现自定义拦截器
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的cms
2010/12/19 PHP
微信公众平台消息接口校验与消息接口响应实例
2014/12/23 PHP
php计算到指定日期还有多少天的方法
2015/04/14 PHP
mysql alter table命令修改表结构实例详解
2016/09/24 PHP
PHP框架Laravel中使用UUID实现数据分表操作示例
2018/05/30 PHP
Yii2框架实现利用mpdf创建pdf文件功能示例
2019/02/08 PHP
TopList标签和JavaScript结合两例
2007/08/12 Javascript
JQUERY设置IFRAME的SRC值的代码
2010/11/30 Javascript
使用Grunt.js管理你项目的应用说明
2013/04/24 Javascript
图解JavaScript中的this关键字
2020/05/28 Javascript
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
2016/07/11 Javascript
Angular 理解module和injector,即依赖注入
2016/09/07 Javascript
Vue框架TypeScript装饰器使用指南小结
2019/02/18 Javascript
微信小程序实现类似微信点击语音播放效果
2020/03/30 Javascript
微信小程序实现拍照画布指定区域生成图片
2019/07/18 Javascript
layer页面跳转,获取html子节点元素的值方法
2019/09/27 Javascript
Javascript执行流程细节原理解析
2020/05/14 Javascript
[03:03]DOTA2校园争霸赛 济南城市决赛欢乐发奖活动
2013/10/21 DOTA
Python3实现从文件中读取指定行的方法
2015/05/22 Python
Linux下将Python的Django项目部署到Apache服务器
2015/12/24 Python
ubuntu安装mysql pycharm sublime
2018/02/20 Python
Python 元类实例解析
2018/04/04 Python
使用DataFrame删除行和列的实例讲解
2018/04/08 Python
Python简单获取二维数组行列数的方法示例
2018/12/21 Python
Python使用微信接入图灵机器人过程解析
2019/11/04 Python
html5.2 dialog简介详解
2018/02/27 HTML / CSS
英国标志性奢侈品牌:Burberry
2016/07/28 全球购物
颇特女士:NET-A-PORTER(直邮中国)
2020/07/11 全球购物
电大学习个人自我评价范文
2013/10/04 职场文书
投标保密承诺书
2014/05/19 职场文书
小学六一儿童节活动方案
2014/08/27 职场文书
倡议书作文
2015/01/19 职场文书
幼儿园百日安全活动总结
2015/05/07 职场文书
护士自荐信范文(2016推荐篇)
2016/01/28 职场文书
Python编程源码报错解决方法总结经验分享
2021/10/05 Python
spring boot实现文件上传
2022/08/14 Java/Android