1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #Changing Individual Pixels
from PIL import Image
import time
#Desable "DecompressionBombWarning"
Image.MAX_IMAGE_PIXELS = None
#Compteur de temps
tmps1=time.time()
print(tmps1)
#Code département
Departement="03"
#Chemin racine des dossiers à traiter
Chemin = "D:\\#GIS\\Inventaire forestier\\"
#Dossier à traiter
Dossier = Chemin + Departement + "\\"
#Image téléchargée
NomImTel = "Base_" + Departement + ".png"
#Ouverture de l'image téléchargée
ImTel = Image.open(Dossier + NomImTel)
ImTel = ImTel.convert('RGB')
width, height = ImTel.size
print(str(width)+","+str(height))
Dossier = "D:\\#GIS\\Inventaire forestier\\03\\"
FormatImage = ".png"
#Liste des codes couleurs associés au type de forêt
CodesCouleurListe=[\
["JpCrI",229,196,93],\
["Fp",1,120,56],\
["Cdp",0,77,46],\
["Csp",102,128,64],\
["Hp",0,185,168],\
["Cp",64,255,38],\
["Rp",145,86,51],\
["Afp",175,202,89],\
["Mf",0,147,27],\
["P",255,255,50],\
["Cpi",128,128,255],\
["Pmp",191,38,255],\
["Psp",153,38,255],\
["PlPnp",57,11,215],\
["Pap",255,26,205],\
["PcPcp",135,97,250],\
["App",216,152,255],\
["Mpp",255,153,255],\
["Sep",26,230,230],\
["Mp",97,128,255],\
["Dp",51,153,255],\
["Acp",169,255,255],\
["Mac",1,146,159],\
["Mc",0,0,255],\
["Mfpc",255,152,51],\
["Mcpf",255,64,51],\
["FocrI",178,158,147],\
["Fofp",204,255,191],\
["Focp",153,179,204],\
["Fomfc",255,209,56],\
["Ll",255,230,191],\
["Fh",255,255,185]]
#Boucle sur la liste
for Foret, Red, Green, Blue in CodesCouleurListe:
#Boolean de test présence pixel
FindPixel=False
ImType=Image.new('RGBA',(width,height)) #Création d'une instance d'image à le même taille que l'image de base
for x in range(width): #Itération du X pixels
for y in range(height): #Itération du Y pixels
r,g,b=ImTel.getpixel((x, y))
if r==Red and g==Green and b==Blue:
FindPixel=True
ImType.putpixel((x, y), (145, 86, 51))
#Si la condition est vrai enregistrement image
if FindPixel==True:
Adresse = Dossier + Departement + "_" + Foret + FormatImage
ImType.save(Adresse)
tmps2=time.time()
print(tmps2)
print ("Temps d'execution = %f" %tmps2) |
Partager