Filtrage de couleur


In [20]:
from PIL import Image               # to load images
from IPython.display import display # to display images
import os
from google.colab import drive
drive.mount('/content/gdrive')
print(os.listdir('./gdrive/My Drive/'))
print(os.getcwd())
imgage_originale = Image.open("./gdrive/My Drive/image1.png")
display(imgage_originale)
largeur=imgage_originale.size[0]
hauteur=imgage_originale.size[1]
image_filtrage_rvb = Image.new("RGB",(largeur,hauteur))

for x in range(largeur):    # pour tous les pixels
  for y in range(hauteur):
    rouge,vert,bleu,i=imgage_originale.getpixel((x,y))
    image_filtrage_rvb.putpixel((x, y), (rouge,0,0))
display(image_filtrage_rvb)


Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
['Colab Notebooks', 'ecole_yves', 'image1.png']
/content

Filtrage par convolution


In [21]:
image_filtrage_convolution = Image.new("RGB",(largeur,hauteur))
for y in range(1,hauteur-1):
	for x in range(1,largeur-1):
		pix0 = img.getpixel((x,y))
		pix1 = img.getpixel((x-1,y-1))
		pix2 = img.getpixel((x,y-1))
		pix3 = img.getpixel((x+1,y-1))
		pix4 = img.getpixel((x-1,y))
		pix5 = img.getpixel((x+1,y))
		pix6 = img.getpixel((x-1,y+1))
		pix7 = img.getpixel((x,y+1))
		pix8 = img.getpixel((x+1,y+1))
		r = 8*pix0[0]-pix1[0]-pix2[0]-pix3[0]-pix4[0]-pix5[0]-pix6[0]-pix7[0]-pix8[0]
		v = r
		b = r
		image_filtrage_convolution.putpixel((x,y),(r,v,b))
display(image_filtrage_convolution)