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

到此这篇关于python 实现图片特效处理的文章就介绍到这了,更多相关python 图片特效内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python图片处理之图片采样处理详解

    目录 一.图像采样处理原理 二.图像采样实现 三.图像局部采样处理 四.总结 一.图像采样处理原理 图像采样(Image Sampling)处理是将一幅连续图像在空间上分割成M×N个网格,每个网格用一个亮度值或灰度值来表示,其示意图如图9-1所示. 图像采样的间隔越大,所得图像像素数越少,空间分辨率越低,图像质量越差,甚至出现马赛克效应:相反,图像采样的间隔越小,所得图像像素数越多,空间分辨率越高,图像质量越好,但数据量会相应的增大.图9-2展示了不同采样间隔的“Lena”图,其中图(a)为原始

  • 用Python PIL实现几个简单的图片特效

    导入 numpy .PIL numpy用来做矩阵运算,PIL用来读取图片. import numpy as np from PIL import Image 读取图片,然后转换成RGB模式存在矩阵里 im = Image.open(imagename).convert('RGB') arr = np.array(im) 查看arr的shape,可以看到arr是个3维的数组,数组大小等于 长*宽*3 In [566]: arr.shape Out[566]: (313, 450, 3) 每个像素有

  • Python Opencv实现图片切割处理

    本文实例为大家分享了Python Opencv实现图片的切割处理,供大家参考,具体内容如下 Opencv对图片的切割: 方法一: import os from PIL import Image def splitimage(src, rownum, colnum, dstpath):     img = Image.open(src)     w, h = img.size     if rownum <= h and colnum <= w:         print('Original

  • Python图片处理模块PIL操作方法(pillow)

    一.PIL的基本概念: PIL中所涉及的基本概念有如下几个:通道(bands).模式(mode).尺寸(size).坐标系统(coordinate system).调色板(palette).信息(info)和滤波器(filters). 1.通道 每张图片都是由一个或者多个数据通道构成.PIL允许在单张图片中合成相同维数和深度的多个通道. 以RGB图像为例,每张图片都是由三个数据通道构成,分别为R.G和B通道.而对于灰度图像,则只有一个通道. 对于一张图片的通道数量和名称,可以通过方法getban

  • python图片处理库Pillow实现简单PS功能

    目录 安装 基本操作 打开图像 转换格式 展示图片 剪裁 合并 缩略图 旋转 滤镜 二次创作 画线 文字 总结 文 | 豆豆 来源:Python 技术「ID: pythonall」 在我们的日常生活和工作中有不少场景需要简单处下理图片,很多人都是依赖 PS.美图秀秀等各种图像处理工具,殊不知在你打开软件的一瞬间 Python 就已经将图片处理完了.听起来是不是很神奇,正所谓是 Python 在手,啥也不愁. 安装 老规矩,先通过 pip 安装到本地机器. pip install Pillow 基

  • Python编程OpenCV和Numpy图像处理库实现图片去水印

    目录 OpenCV + Numpy 函数简介 色彩转换 PIL + itertools 大家好,我是小五 前一阵给大家分享了,Python如何给图片加水印.评论区就有小伙伴问,可不可使用Python去除图片水印的方法呢? 这个肯定有啊,不过由于图片水印的种类有很多,今天我们先讲最简单的一种. 即上图中的①类水印,这种水印存在白色背景上的文档里,水印是灰色,需要保留的文字是黑色. 这种通常可以进行简单的亮度/对比度转换,直到水印消失并降低亮度以进行补偿. 参考别人的方法,我发现可以用多种方法去除水

  • python 实现图片特效处理

    目录 一.黑白特效 二.流年特效 三.旧电影特效 四.反色特效 前言: 对于 ​图片处理​,在日常生活中我们常常能够看到. 比如发个朋友圈之前,我们需要给自己的​照片加个滤镜​:在上传头像时候,需要​对照片进行裁剪​,这些都是图片的处理. 待处理的原图: 一.黑白特效 将图片处理后,变为黑白颜色 把像素的R,G,B三个通道数值都置为:​​r*0.299+g*0.587+b*0.114​​ 效果 黑白特效: 代码:  #!/usr/bin/env python # encoding: utf-8

  • Python 实现图像特效中的油画效果

    目录 一 基本原理 二 代码实现 三 总体实现代码以及保存  在前面的文章Python 计算机视觉(十五)-- 图像特效处理中我已经介绍了大部分的图像的特效处理,但还是忽略了油画特效的处理,在本篇文章中简单介绍一下油画特效的基本原理以及代码实现,感兴趣的小伙伴可以跟着码一遍代码,或者使用代码直接运行查看一下效果就行. 一 基本原理 如下面的两幅图所示,油画用对了地方会使得图像一下子显得文艺起来了呢! 拍出的图像 转化为油画 那么将一幅图像转化为油画类型的图案是怎么实现的呢?为了将一幅普通的图像转

  • jquery淡化版banner异步图片文字效果切换图片特效

    复制代码 代码如下: <pre code_snippet_id="280064" snippet_file_name="blog_20140408_1_8982765" name="code" class="html"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org

  • 分享精心挑选的23款美轮美奂的jQuery 图片特效插件

    下面向大家分享精心挑选的23款优秀 jQuery 图片特效插件,带给你美轮美奂的图片展示效果. 01. 3D Image Slider 非常非常酷的 3D 图片滑动效果,有五种绚丽的效果演示. 在线演示 下载源码 02. Portfolio Image Navigator 精致的图片导航效果,通过四个方向的箭头控制,适合作品展示的应用场合. 在线演示 下载源码 03. Image Transitions 非常精美的图片切换效果,有 Flip.Multi-flip.Rotation.Cube.Un

  • python获取图片颜色信息的方法

    本文实例讲述了python获取图片颜色信息的方法.分享给大家供大家参考.具体分析如下: python的pil模块可以从图片获得图片每个像素点的颜色信息,下面的代码演示了如何获取图片所有点的颜色信息和每种颜色的数量. from PIL import Image image = Image.open("jb51.gif") image.getcolors() 返回结果如下 复制代码 代码如下: ..., (44, (72, 64, 55, 255)), (32, (231, 208, 14

  • python将图片文件转换成base64编码的方法

    本文实例讲述了python将图片文件转换成base64编码的方法.分享给大家供大家参考.具体实现方法如下: import base64 f=open(r'c:\jb51.gif','rb') #二进制方式打开图文件 ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码 f.close() 调用方法如下: 复制代码 代码如下: <img src="R0lGODlh1wBOAPcAAAAAAP///7a4u+jq7bG1ucrN0N7g4tLU

  • Python读取图片属性信息的实现方法

    本文是利用Python脚本读取图片信息,有几个说明如下: 1.没有实现错误处理 2.没有读取所有信息,大概只有 GPS 信息.图片分辨率.图片像素.设备商.拍摄设备等 3.简单修改后应该能实现暴力修改图片的 GPS 信息 4.但对于本身没有 GPS 信息的图片,实现则非常复杂,需要仔细计算每个描述符的偏移量 脚本运行后,读取结果如下 脚本读取的信息 这里和 Windows 属性查看器读到的内容完全一致 图片信息1 图片信息2 源码如下 # -*- coding:utf-8 -*- import

  • Android编程中图片特效处理方法小结

    本文实例总结了Android编程中图片特效处理方法.分享给大家供大家参考,具体如下: 这里介绍的Android图片处理方法包括: 转换 -  drawable To  bitmap 缩放 -  Zoom 圆角 -  Round Corner 倒影 -  Reflected bitmapPrcess  code: package com.learn.games; import android.graphics.Bitmap; import android.graphics.Canvas; impo

  • Python 爬虫图片简单实现

    Python 爬虫图片简单实现 经常在逛知乎,有时候希望把一些问题的图片集中保存起来.于是就有了这个程序.这是一个非常简单的图片爬虫程序,只能爬取已经刷出来的部分的图片.由于对这一部分内容不太熟悉,所以只是简单说几句然后记录代码,不做过多的讲解.感兴趣的可以直接拿去用.亲测对于知乎等网站是可用的. 上一篇分享了通过url打开图片的方法,目的就是先看看爬取到的图片时什么样,然后再筛选一下保存. 这里用到了requests库来获取页面信息,需要注意的是,获取页面信息的时候需要一个header,用以把

  • python让图片按照exif信息里的创建时间进行排序的方法

    本文实例讲述了python让图片按照exif信息里的创建时间进行排序的方法.分享给大家供大家参考.具体分析如下: 我们经常会从不同的设备里取出照片,比如照相机,手机,iphone等等,操作系统记录的创建日期经常 会因为拷贝等原因变动,下面的代码可以给图片按照exif里的创建时间进行排序,非常有用. 复制代码 代码如下: import os import shutil import Image from PIL.ExifTags import TAGS def print_all_known_ex

随机推荐