python?實(shí)現(xiàn)圖片特效處理
前言:
對于 ?圖片處理?,在日常生活中我們常常能夠看到。
比如發(fā)個(gè)朋友圈之前,我們需要給自己的?照片加個(gè)濾鏡?;在上傳頭像時(shí)候,需要?對照片進(jìn)行裁剪?,這些都是圖片的處理。
待處理的原圖:

一、黑白特效
- 將圖片處理后,變?yōu)楹诎最伾?/li>
- 把像素的R,G,B三個(gè)通道數(shù)值都置為:?
?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 + '圖片轉(zhuǎn)換特效之黑白')
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()二、流年特效
- 將圖片處理后,變?yōu)榱髂晏匦?/li>
- 把R通道的數(shù)值開平方,然后乘以一個(gè)參數(shù)
- 效果
流年特效:

代碼:
#!/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 + '圖片轉(zhuǎn)換特效之流年')
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()三、舊電影特效
- 將圖片處理后,變?yōu)榕f電影特效
- 把像素的R,G,B三個(gè)通道數(shù)值,3個(gè)通道的分別乘以3個(gè)參數(shù)后求和,最后把超過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 + '圖片轉(zhuǎn)換特效之舊電影')
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()四、反色特效
- 將圖片處理后,變?yōu)榉瓷匦?/li>
- 這個(gè)最簡單了,用255減去每個(gè)通道的原來的數(shù)值
- 效果
反色特效:

代碼:
#!/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 + '圖片轉(zhuǎn)換特效之反色')
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()到此這篇關(guān)于python 實(shí)現(xiàn)圖片特效處理的文章就介紹到這了,更多相關(guān)python 圖片特效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用python socket模塊實(shí)現(xiàn)簡單的文件下載
這篇文章主要介紹了如何使用python socket模塊實(shí)現(xiàn)簡單的文件下載,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
Python使用lambda表達(dá)式對字典排序操作示例
這篇文章主要介紹了Python使用lambda表達(dá)式對字典排序操作,結(jié)合實(shí)例形式分析了lambda表達(dá)式實(shí)現(xiàn)字典按鍵排序、按值排序、多條件排序相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
解決Pycharm下面出現(xiàn)No R interpreter defined的問題
今天小編就為大家分享一篇解決Pycharm下面出現(xiàn)No R interpreter defined的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
scrapy框架攜帶cookie訪問淘寶購物車功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了scrapy框架攜帶cookie訪問淘寶購物車,本文通過實(shí)例代碼圖文詳解給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
python機(jī)器學(xué)習(xí)邏輯回歸隨機(jī)梯度下降法
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)邏輯回歸隨機(jī)梯度下降法的詳細(xì)講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Python+wxPython實(shí)現(xiàn)個(gè)人鏈接收藏夾
這篇文章主要介紹了如何使用wxPython和XML數(shù)據(jù)源創(chuàng)建一個(gè)具有按鈕和Web視圖的應(yīng)用程序窗口,以便輕松管理和訪問各種網(wǎng)頁鏈接,感興趣的可以了解下2023-08-08

