python怎么实现图片特效处理
python怎么实现图片特效处理
这篇文章主要介绍了python怎么实现图片特效处理的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python怎么实现图片特效处理文章都会有所收获,下面我们一起来看看吧。
前言:
对于 图片处理,在日常生活中我们常常能够看到。
比如发个朋友圈之前,我们需要给自己的照片加个滤镜;在上传头像时候,需要对照片进行裁剪,这些都是图片的处理。
待处理的原图:
一、黑白特效
将图片处理后,变为黑白颜色
把像素的R,G,B三个通道数值都置为:
r*0.299+g*0.587+b*0.114
效果
黑白特效:
代码:
#!/usr/bin/envpython#encoding:utf-8importnumpyasnpfromPILimportImageclasspicture:'''ThisisamainClass,thefilecontainsalldocuments.OnedocumentcontainsparagraphsthathaveseveralsentencesItloadstheoriginalfileandconvertstheoriginalfiletonewcontentThenthenewcontentwillbesavedbythisclass'''def__init__(self):self.path='assets/picture.jpeg'defhello(self):'''Thisisawelcomespeech:return:self'''print('*'*50)print(''*20+'图片转换特效之黑白')print(''*5+'作者:autofelixDate:2022-01-1713:14')print('*'*50)returnselfdefrun(self):'''Theprogramentry'''im=self.to_black_white()im.show()im.save('assets/black_white.jpeg')defto_black_white(self):'''Picturetoblackwhite'''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)returnImage.fromarray(np.array(im).astype('uint8'))if__name__=='__main__':picture().hello().run()
二、流年特效
将图片处理后,变为流年特效
把R通道的数值开平方,然后乘以一个参数
效果
流年特效:
代码:
#!/usr/bin/envpython#encoding:utf-8importnumpyasnpfromPILimportImageclasspicture:'''ThisisamainClass,thefilecontainsalldocuments.OnedocumentcontainsparagraphsthathaveseveralsentencesItloadstheoriginalfileandconvertstheoriginalfiletonewcontentThenthenewcontentwillbesavedbythisclass'''def__init__(self):self.path='assets/picture.jpeg'defhello(self):'''Thisisawelcomespeech:return:self'''print('*'*50)print(''*20+'图片转换特效之流年')print(''*5+'作者:autofelixDate:2022-01-1713:14')print('*'*50)returnselfdefrun(self):'''Theprogramentry'''im=self.fleeting()im.show()im.save('assets/fleeting.jpeg')deffleeting(self,params=12):'''Picturetofleeting'''im=np.asarray(Image.open(self.path).convert('RGB'))im1=np.sqrt(im*[1.0,0.0,0.0])*paramsim2=im*[0.0,1.0,1.0]im=im1+im2returnImage.fromarray(np.array(im).astype('uint8'))if__name__=='__main__':picture().hello().run()
三、旧电影特效
将图片处理后,变为旧电影特效
把像素的R,G,B三个通道数值,3个通道的分别乘以3个参数后求和,最后把超过255的值置为255
效果
旧电影特效:
代码:
#!/usr/bin/envpython#encoding:utf-8importnumpyasnpfromPILimportImageclasspicture:'''ThisisamainClass,thefilecontainsalldocuments.OnedocumentcontainsparagraphsthathaveseveralsentencesItloadstheoriginalfileandconvertstheoriginalfiletonewcontentThenthenewcontentwillbesavedbythisclass'''def__init__(self):self.path='assets/picture.jpeg'defhello(self):'''Thisisawelcomespeech:return:self'''print('*'*50)print(''*20+'图片转换特效之旧电影')print(''*5+'作者:autofelixDate:2022-01-1713:14')print('*'*50)returnselfdefrun(self):'''Theprogramentry'''im=self.old_film()im.show()im.save('assets/old_film.jpeg')defold_film(self):'''Picturetooldfilm'''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)returnImage.fromarray(np.array(im).astype('uint8'))if__name__=='__main__':picture().hello().run()
四、反色特效
将图片处理后,变为反色特效
这个最简单了,用255减去每个通道的原来的数值
效果
反色特效:
代码:
#!/usr/bin/envpython#encoding:utf-8importnumpyasnpfromPILimportImageclasspicture:'''ThisisamainClass,thefilecontainsalldocuments.OnedocumentcontainsparagraphsthathaveseveralsentencesItloadstheoriginalfileandconvertstheoriginalfiletonewcontentThenthenewcontentwillbesavedbythisclass'''def__init__(self):self.path='assets/picture.jpeg'defhello(self):'''Thisisawelcomespeech:return:self'''print('*'*50)print(''*20+'图片转换特效之反色')print(''*5+'作者:autofelixDate:2022-01-1713:14')print('*'*50)returnselfdefrun(self):'''Theprogramentry'''im=self.reverse()im.show()im.save('assets/reverse.jpeg')defreverse(self):'''Picturetoreverse'''im=255-np.asarray(Image.open(self.path).convert('RGB'))returnImage.fromarray(np.array(im).astype('uint8'))if__name__=='__main__':picture().hello().run()
关于“python怎么实现图片特效处理”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python怎么实现图片特效处理”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道。
推荐阅读
-
Lightly IDE 快捷键:Python 开发者必学的效率提升操作
-
GitHub Codespaces 模板配置:快速初始化项目环境的技巧
-
Python 类型注解进阶:mypy 静态类型检查与 IDE 集成
-
Python 3.12 模式匹配增强:结构分解与多分支逻辑简化实战
-
Lightly IDE 快捷键定制:Python 开发者专属效率提升方案
-
Python 装饰器高级用法:类装饰器与元类结合实践
-
Python 生成器表达式优化:内存占用与迭代效率平衡技巧
-
Python 类型注解深度:Protocol 协议与泛型类型约束实践
-
Python 3.12 新特性解析:模式匹配增强与性能优化实战
-
Lightly IDE 深度评测:轻量级 Python 开发工具是否适合团队协作?
-
Lightly IDE 快捷键:Python 开发者必学的效率提升操作
-
GitHub Codespaces 模板配置:快速初始化项目环境的技巧
-
Python 类型注解进阶:mypy 静态类型检查与 IDE 集成
-
Python 3.12 模式匹配增强:结构分解与多分支逻辑简化实战
-
Lightly IDE 快捷键定制:Python 开发者专属效率提升方案
-
Python 装饰器高级用法:类装饰器与元类结合实践
-
Python 生成器表达式优化:内存占用与迭代效率平衡技巧
-
Python 类型注解深度:Protocol 协议与泛型类型约束实践
-
Python 3.12 新特性解析:模式匹配增强与性能优化实战
-
Lightly IDE 深度评测:轻量级 Python 开发工具是否适合团队协作?
-
Lightly IDE 快捷键:Python 开发者必学的效率提升操作
-
GitHub Codespaces 模板配置:快速初始化项目环境的技巧
-
Python 类型注解进阶:mypy 静态类型检查与 IDE 集成
-
Python 3.12 模式匹配增强:结构分解与多分支逻辑简化实战
-
Lightly IDE 快捷键定制:Python 开发者专属效率提升方案
-
Python 装饰器高级用法:类装饰器与元类结合实践
-
Python 生成器表达式优化:内存占用与迭代效率平衡技巧
-
Python 类型注解深度:Protocol 协议与泛型类型约束实践
-
Python 3.12 新特性解析:模式匹配增强与性能优化实战
-
Lightly IDE 深度评测:轻量级 Python 开发工具是否适合团队协作?