Python Pillow - Adicionando Filtros a uma Imagem
o ImageFilter module contém definições para um conjunto predefinido de filtros, que usamos com Image.filter()método. Esses filtros são usados para alterar a aparência e o comportamento da imagem.
Exemplo
O exemplo abaixo é Filtrando uma imagem -
from PIL import Image, ImageFilter
im = Image.open('jungleSaf2.jpg')
im1 = im.filter(ImageFilter.BLUR)
im1.show()
im2 = im.filter(ImageFilter.MinFilter(3))
im2.show()
im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3)
im3.show()
No programa acima, usamos o MinFilter()método, que é usado para criar um filtro mínimo. Ele escolhe o menor valor de pixel em uma janela com o tamanho fornecido.
ImageFilter.MinFilter(size=3)
Onde,
size - O tamanho do kernel, em pixels.
Resultado
Se você salvar o programa acima e executar, ele mostra a imagem original, a imagem borrada e a imagem borrada com MinFilter usando o utilitário de exibição PNG padrão, como segue -
Original Image

Blurred Image

Image blurred with mini filter

Filtros
A versão atual da biblioteca de travesseiros fornece o conjunto mencionado abaixo de filtros de aprimoramento de imagem predefinidos.
BLUR
CONTOUR
DETAIL
EDGE_ENHANCE
EDGE_ENHANCE_MORE
EMBOSS
FIND_EDGES
SHARPEN
SMOOTH
SMOOTH_MORE
Exemplo
O exemplo de python a seguir aplica o filtro de desfoque em uma imagem, salva-a e exibe-a usando o utilitário de exibição PNG padrão -
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(BLUR)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado

Da mesma forma, para o image.filter() método, você pode passar qualquer um dos seguintes parâmetros para obter as respectivas saídas -
CONTOUR
DETAIL
EDGE_ENHANCE
EDGE_ENHANCE_MORE
EMBOSS
FIND_EDGES
SMOOTH
SMOOTH_MORE
SHARPEN
Método Python img.filter (CONTOUR)
O exemplo de python a seguir aplica o filtro CONTOUR à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(CONTOUR)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostrará a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (DETAIL)
O seguinte exemplo python aplica o filtro DETAIL à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(DETAIL)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostra a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (EDGE_ENHANCE)
O exemplo de python a seguir aplica o filtro EDGE_ENHANCE à imagem fornecida -
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(EDGE_ENHANCE)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostra a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (EDGE_ENHANCE_MORE)
O exemplo de python a seguir aplica o filtro EDGE_ENHANCE_MORE à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(EDGE_ENHANCE_MORE)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostra a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (EMBOSS)
O exemplo de python a seguir aplica o filtro EMBOSS à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(EMBOSS)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostrará a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (FIND_EDGES)
O exemplo de python a seguir aplica o filtro FIND_EDGES à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(FIND_EDGES)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostrará a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (SMOOTH)
O exemplo de python a seguir aplica o filtro SMOOTH à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(SMOOTH)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostrará a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

Método Python img.filter (SHARPEN)
O exemplo de python a seguir aplica o filtro SHARPEN à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(SHARPEN)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostrará a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image

O exemplo de python a seguir aplica o filtro SHARPEN à imagem fornecida.
Exemplo
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filter from pillow
from PIL.ImageFilter import (
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
)
#Create image object
img = Image.open('images/cat.jpg')
#Applying the blur filter
img1 = img.filter(SHARPEN)
img1.save('images/ImageFilter_blur.jpg')
img1.show()
Resultado
Se você salvar o programa acima e executar, ele mostrará a imagem original e a imagem filtrada usando o utilitário de exibição PNG padrão, como segue -
Original image

Filtered image
