python 实现图片特效处理

作者:autofelix 时间:2021-04-20 05:34:22 

前言:

对于 图片处理,在日常生活中我们常常能够看到。

比如发个朋友圈之前,我们需要给自己的照片加个滤镜;在上传头像时候,需要对照片进行裁剪,这些都是图片的处理。

待处理的原图:

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()

来源:https://blog.51cto.com/autofelix/4938261

标签:python,实现,图片,特效,处理
0
投稿

猜你喜欢

  • 苹果的“创新”

    2010-01-12 13:45:00
  • PHP的mysqli_set_charset()函数讲解

    2023-07-11 06:22:17
  • Oracle入侵常用操作命令整理

    2009-03-04 11:11:00
  • 网页语言编码及asp乱码问题解决方案

    2008-01-31 13:21:00
  • asp 删除数据并同时删除图片的代码

    2011-02-28 10:39:00
  • Python实现迪杰斯特拉算法过程解析

    2022-08-14 09:55:42
  • 关于Keras模型可视化教程及关键问题的解决

    2021-03-19 10:39:07
  • Go 字符串格式化的实例代码详解

    2023-08-05 14:05:30
  • java JSP开发之Spring中Bean的使用

    2023-06-16 07:35:08
  • Python实现统计单词出现的个数

    2022-11-01 12:19:30
  • Python面向对象总结及类与正则表达式详解

    2021-08-12 13:23:34
  • 使用DW中遇到的常见问题详解

    2008-03-18 16:27:00
  • Python实现图像去噪方式(中值去噪和均值去噪)

    2023-04-15 15:38:13
  • asp如何让我的网页自动适应客户端的屏幕分辨率?

    2010-05-13 16:38:00
  • ASP编程如何执行存储过程?

    2010-03-17 20:56:00
  • 解析:MySQL对“服务器端光标”的限制

    2008-11-27 16:22:00
  • python实现点对点聊天程序

    2023-10-27 11:42:02
  • PHP引用符&的用法详细解析

    2023-10-17 17:25:53
  • python中protobuf和json互相转换应用处理方法

    2023-03-15 11:50:55
  • 用玩票的心态瞎猜豆瓣的思路

    2008-08-18 21:14:00
  • asp之家 网络编程 m.aspxhome.com