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设置windows桌面壁纸的实现代码
Jan 28 Python
Python 错误和异常小结
Oct 09 Python
python批量导出导入MySQL用户的方法
Nov 15 Python
python中enumerate函数用法实例分析
May 20 Python
Python利用Beautiful Soup模块创建对象详解
Mar 27 Python
python自动12306抢票软件实现代码
Feb 24 Python
python绘制多个曲线的折线图
Mar 23 Python
使用PM2+nginx部署python项目的方法示例
Nov 07 Python
Python利用for循环打印星号三角形的案例
Apr 12 Python
Python使用jupyter notebook查看ipynb文件过程解析
Jun 02 Python
Python非单向递归函数如何返回全部结果
Dec 18 Python
python库Tsmoothie模块数据平滑化异常点抓取
Jun 10 Python
教你使用Python获取QQ音乐某个歌手的歌单
Python os和os.path模块详情
如何通过一篇文章了解Python中的生成器
Python pyecharts绘制条形图详解
Python OpenCV超详细讲解读取图像视频和网络摄像头
基于Python实现股票收益率分析
python实现对doc、txt、xls等文档的读写操作
Apr 02 #Python
You might like
php获取目标函数执行时间示例
2014/03/04 PHP
PHP通过调用新浪API生成t.cn格式短网址链接的方法详解
2019/02/20 PHP
动态加载iframe
2006/06/16 Javascript
javascript下阻止表单重复提交、防刷新、防后退
2007/08/17 Javascript
JavaScript类库D
2010/10/24 Javascript
JQuery扩展插件Validate 5添加自定义验证方法
2011/09/05 Javascript
js控制浏览器全屏示例代码
2014/02/20 Javascript
js实现图片在未加载完成前显示加载中字样
2014/09/03 Javascript
JavaScript中constructor()方法的使用简介
2015/06/05 Javascript
JavaScript Math 对象常用方法总结
2016/04/28 Javascript
详解Node.js模块间共享数据库连接的方法
2016/05/24 Javascript
jQuery改变form表单的action,并进行提交的实现代码
2016/05/25 Javascript
jQuery autoComplete插件两种使用方式及动态改变参数值的方法详解
2016/10/24 Javascript
js实现数组去重方法及效率?Ρ? target=
2017/02/14 Javascript
微信小程序--组件(swiper)详细介绍
2017/06/13 Javascript
React组件中的this的具体使用
2018/02/28 Javascript
node puppeteer(headless chrome)实现网站登录
2018/05/09 Javascript
小程序接口的promise化的实现方法
2019/12/11 Javascript
js实现计算器功能
2020/08/10 Javascript
pygame学习笔记(2):画点的三种方法和动画实例
2015/04/15 Python
Python中使用strip()方法删除字符串中空格的教程
2015/05/20 Python
python中私有函数调用方法解密
2016/04/29 Python
Python决策树和随机森林算法实例详解
2018/01/30 Python
python 请求服务器的实现代码(http请求和https请求)
2018/05/25 Python
python中将\\uxxxx转换为Unicode字符串的方法
2018/09/06 Python
Python对Excel按列值筛选并拆分表格到多个文件的代码
2019/11/05 Python
python 实现两个线程交替执行
2020/05/02 Python
英国体育器材进口商店:UK Sport Imports
2017/03/14 全球购物
岳父生日宴会答谢词
2014/01/13 职场文书
保安队长职务说明书
2014/02/23 职场文书
《陋室铭》教学反思
2014/02/26 职场文书
安全宣传标语
2014/06/10 职场文书
感恩主题班会教案
2015/08/12 职场文书
电力培训学习心得体会
2016/01/11 职场文书
如果用一句诗总结你的上半年,你会用哪句呢?
2019/07/16 职场文书
原生Javascript+HTML5一步步实现拖拽排序
2021/06/12 Javascript