Oreiller Python - Ajout de filtres à une image

le ImageFilter module contient des définitions pour un ensemble prédéfini de filtres, que nous avons utilisé avec Image.filter()méthode. Ces filtres sont utilisés pour modifier l'apparence et la sensation de l'image.

Exemple

L'exemple ci-dessous est Filtrer une image -

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

Dans le programme ci-dessus, nous avons utilisé le MinFilter()méthode, qui est utilisée pour créer un filtre minimum. Il sélectionne la valeur de pixel la plus basse dans une fenêtre avec la taille donnée.

ImageFilter.MinFilter(size=3)

Où,

size - La taille du noyau, en pixels.

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine, l'image floue et l'image floue avec MinFilter à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original Image

Blurred Image

Image blurred with mini filter

Filtres

La version actuelle de la bibliothèque d'oreillers fournit un ensemble de filtres d'amélioration d'image prédéfinis mentionnés ci-dessous.

  • BLUR

  • CONTOUR

  • DETAIL

  • EDGE_ENHANCE

  • EDGE_ENHANCE_MORE

  • EMBOSS

  • FIND_EDGES

  • SHARPEN

  • SMOOTH

  • SMOOTH_MORE

Exemple

L'exemple python suivant applique le filtre de flou sur une image, l'enregistre et l'affiche à l'aide de l'utilitaire d'affichage PNG standard -

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

Production

De la même manière, à la image.filter() méthode, vous pouvez passer l'un des paramètres suivants pour obtenir les sorties respectives -

  • CONTOUR

  • DETAIL

  • EDGE_ENHANCE

  • EDGE_ENHANCE_MORE

  • EMBOSS

  • FIND_EDGES

  • SMOOTH

  • SMOOTH_MORE

  • SHARPEN

Méthode Python img.filter (CONTOUR)

L'exemple python suivant applique le filtre CONTOUR à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (DETAIL)

L'exemple python suivant applique le filtre DETAIL à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (EDGE_ENHANCE)

L'exemple python suivant applique le filtre EDGE_ENHANCE à l'image donnée -

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (EDGE_ENHANCE_MORE)

L'exemple python suivant applique le filtre EDGE_ENHANCE_MORE à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (EMBOSS)

L'exemple python suivant applique le filtre EMBOSS à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (FIND_EDGES)

L'exemple python suivant applique le filtre FIND_EDGES à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (SMOOTH)

L'exemple python suivant applique le filtre SMOOTH à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

Méthode Python img.filter (SHARPEN)

L'exemple python suivant applique le filtre SHARPEN à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image

L'exemple python suivant applique le filtre SHARPEN à l'image donnée.

Exemple

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

Production

Si vous enregistrez et exécutez le programme ci-dessus, il affiche l'image d'origine et l'image filtrée à l'aide de l'utilitaire d'affichage PNG standard, comme suit -

Original image

Filtered image