python 实现图片特效处理
作者:autofelix 发布时间:2021-04-20 05:34:22
标签:python,实现,图片,特效,处理
前言:
对于 图片处理,在日常生活中我们常常能够看到。
比如发个朋友圈之前,我们需要给自己的照片加个滤镜;在上传头像时候,需要对照片进行裁剪,这些都是图片的处理。
待处理的原图:
一、黑白特效
将图片处理后,变为黑白颜色
把像素的R,G,B三个通道数值都置为:
r*0.299+g*0.587+b*0.114
效果
黑白特效:
代码:
#!/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通道的数值开平方,然后乘以一个参数
效果
流年特效:
代码:
#!/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
效果
旧电影特效:
代码:
#!/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减去每个通道的原来的数值
效果
反色特效:
代码:
#!/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
0
投稿
猜你喜欢
- 用python连接Oracle是总是乱码,最有可能的是oracle客户端的字符编码设置不对。本人是在进行数据插入的时候总是报关键字"
- 在使用reflect包获取函数,并调用时,总出现这个报错:panic: reflect: call of reflect.Value.Cal
- 背景基本上只要是做后台开发,都会接触到分页这个需求或者功能吧。基本上大家都是会用MySQL的LIMIT来处理,而且我现在负责的项目也是这样写
- 大概五年前吧,我那时还在为一家约会网站做开发工作。他们是早期创业公司,但他们也开始拥有了一些稳定用户量。不像其他约会网站,这家公司向来以洁身
- pandas读取txt文件读取txt文件需要确定txt文件是否符合基本的格式,也就是是否存在\t,,,等特殊的分隔符一般txt文件长成这个样
- tensorFlow中主要包括了三种不同的并行策略,其分别是数据并行、模型并行、模型计算流水线并行,具体参考Tenssorflow白皮书,在
- 这篇文章主要介绍了python如何使用jt400.jar包代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- Python中的模块(.py文件)在创建之初会自动加载一些内建变量,__name__就是其中之一。Python模块中通常会定义很多变量和函数
- 1. 开始Python 中可以进行网页解析的库有很多,常见的有 BeautifulSoup 和 lxml 等。在网上玩爬虫的文章通常都是介绍
- Python Socket模块中包含一些有用IP转换函数,说明如下:socket.ntohl(x) // 类似于
- MySQL使用环境变量TMPDIR的值作为保存临时文件的目录的路径名。如果未设置TMPDIR,MySQL将使用系统的默认值,通常为/tmp、
- JavaScript ES6之前的还没有Class类的概念,生成实例对象的传统方法是通过构造函数。例如:function Mold(a,b)
- http 模块简介Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块。http.client 是一个底层的 HTTP
- 直接奔入主题看下面pywebio程序,实现了Python的简陋在线编辑器from pywebio.input import *from py
- 本文实例为大家分享了python实现分页效果展示的具体代码,供大家参考,具体内容如下难点:清空Layout#!/usr/bin/python
- 本文实例讲述了Python2.7+pytesser实现简单验证码的识别方法。分享给大家供大家参考,具体如下:首先,安装Python2.7版本
- 最近刚好在学习python+scrapy的爬虫技术,因为mac是自带python2.7的,所以安装3.5版本有两种方法,一种是升级,一种是额
- 对于管理系统,常常需要展示列表数据,我们对于列表内的数据常常需要查找、过滤、排序等操作,其中查找等操作大部分是在后台进行的。django r
- 在python中读取csv文件时,一般操作如下:import pandas as pdpd.read_csv(filename)该读文件方式
- 在写登录功能的时候看着网上的视频学着做,然后看了源码的时候发现了一些有意思的功能,因此写这一篇笔记分享给大家.1.阅读Django自带用户系