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
投稿
猜你喜欢
- 这方面我还是一个freshman,不过看了一些文章,经过一些实践后也算是有了一些想法。希望如果有这方面的前辈路过的话,能不吝指教。首先,作为
- 1.线性与非线性回归线性回归 Linear Regression:两个变量之间的关系是一次函数关系的—&mdas
- 本文实例讲述了PHP开发实现微信退款功能。分享给大家供大家参考,具体如下:最近在调微信退款接口,发现有许多坑,更大家分享一下① 要是在测试的
- 从某个页面表单中取出信息是ASP编程中常见的问题。但是,遍历通过表单传递的记录会花去多长时间呢?这取决于数据库的大小。简单的GUI界面都可能
- 介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。摘要:
- 可以自动轮换的页签 tabs with auto play fucntion<html><head><meta
- PHP fprintf() 函数实例把一些文本写入到名为 "test.txt" 的文本文件:<?php $numb
- 前言网站登录的时候我们常常会看到随机的验证码需要输入后台验证,如图:现在我们来实现在Django中通过自定制插件来实现随机验证check_c
- 在默认情况下,MySQL搜索不区分大小写(但某些字符集始终区分大小写,如czech)。这意味着,如果你使用col_name LIKE
- 在我们的生活中,只要你睁开眼睛就能看到各种各样的视觉。不同的视觉能给你不同的视觉暗示,同样能给你不同的心理感受。视觉这个话题太泛了,大自然中
- 本文讲述了Python检测网络延迟的代码。分享给大家供大家参考,具体如下:#!/usr/bin/env python # coding: u
- 本文实例讲述了Python编程对列表中字典元素进行排序的方法。分享给大家供大家参考,具体如下:内容目录:1. 问题起源2. 对列表中的字典元
- 1. 需求:用户答题练习,当用户获取所有题目的同时,需要判断用户是否已经做过该题目,如果做过,需要render的时候添加一个“回顾”按钮。2
- 一、删除字符串两端的一种或多种字符#strip()、lstrip()、rstrip()方法;(默认删除空格符)A、list.strip(字符
- 1.函数对象前面我们学习了关于Python中的变量类型,例如int、str、bool、list等等…&hell
- 学习目的 接触验证控件 昨天介绍了SQL SERVER插入数据,但是我们省略了验证输入这一步。以前的做法是用语句逐个判断输入的正确性,如是否
- 起因修改了表结构以后执行python3 manage.py migrate 报错:django.db.utils.OperationalEr
- 将Excel中的数据导入到Access中,前提是在Access中的表已经建好。 dim conn dim co
- 1. 使用输入值初始化列表nums = []rows = eval(input("请输入行数:"))columns =
- 目录楔子paramikoSSHClient 的使用connect:实现远程服务器的连接与认证set_missing_host_key_pol