Interface console, affiche à terminer

This commit is contained in:
xdrm-brackets 2015-09-18 00:33:14 +02:00
parent 2a703386ea
commit 8e517ce681
3 changed files with 210 additions and 101 deletions

View File

@ -5,7 +5,11 @@ import random
class Noise:
# ajout de bruit "poivre & sel" avec un seuil (% de l'image bruité)
# Applique le bruitage de type "Poivre & Sel" sur la matrice de pixels #
########################################################################
# @param pixelMap Matrice de pixel à traiter (modifier)
# @param seuil pourcentage de l'image à bruiter (50% <=> 1 pixel sur 2 est bruité)
#
def SaltAndPepper_set(self, pixelMap, seuil=10):
seuil = float(seuil);
@ -24,34 +28,49 @@ class Noise:
pixelMap[y][x].setRGB(0,0,0);
# enlève le bruit "poivre et sel"
# Applique le débruitage de type "Poivre & Sel" sur la matrice de pixels #
##########################################################################
# @param pixelMap Matrice de pixel à traiter (modifier)
# @param seuil Seuil à partir duquel on doit traiter les pixels (écart entre la moyenne des pixels avoisinant et le pixel concerné)
# @param borne 0 = Noir pur et blanc pur sont enlevés; 255 ou + = tout les pixels sont traités
#
def SaltAndPepper_unset(self, pixelMap, seuil=5, borne=5):
width = len( pixelMap[0] )
height = len( pixelMap )
while seuil >= 1:
seuil /= 100.0
if seuil < 0 or seuil > 255: # si le seuil est incohérent => valeur par défaut (5)
seuil = 5;
while borne >= 100:
borne /= 100.0
if borne < 0 or borne > 255: # si la borne est incohérente => valeur par défaut (5)
borne = 5;
seuil = int( seuil * 256 );
# on parcourt tout les pixels
for y in range(0, len(pixelMap)):
for x in range(0, len(pixelMap[y])):
pMoy = ( pixelMap[y][x].r + pixelMap[y][x].g + pixelMap[y][x].b ) / 3
# traitement si couleur extreme
if pMoy >= 255-borne or pMoy <= borne:
xmin, ymin, xmax, ymap = x, y, x, y;
rMoy, gMoy, bMoy, count = 0.0, 0.0, 0.0, 0 # moyennes des couleurs
rInterval, gInterval, bInterval, rgbInterval = 0, 0, 0, 0 # décalage avec le pixel
# on calcule la moyenne des valeurs R G B du pixel courant
pMoy = ( pixelMap[y][x].r + pixelMap[y][x].g + pixelMap[y][x].b ) / 3
# si couleur proche du blanc ou noir (en fonction de la borne)
if pMoy >= 255-borne or pMoy <= borne:
xmin, ymin, xmax, ymap = x, y, x, y; # les bornes ducarré 3x3 autour du pixel
rMoy, gMoy, bMoy, count = 0.0, 0.0, 0.0, 0 # initialisation des variables de moyennes et de total
rInterval, gInterval, bInterval, rgbInterval = 0, 0, 0, 0 # initialisation des variables d'intervalles entre les couleurs
# GESTION DES ANGLES
# ordonnées: borne inférieure
if y-1 > -1:
ymin = y-1
# ordonnées: borne supérieure
if y+1 < height:
ymax = y+1
# abscisses: borne inférieure
if x-1 > -1:
xmin = x-1
# abscisses: borne supérieure
if x+1 < width:
xmax = x+1
@ -64,27 +83,34 @@ class Noise:
# bMoy += p.b;
# count += 1
for j in pixelMap[ymin:ymax]: # on parcourt les pixels autour
# on parcourt le carré de 3x3
for j in pixelMap[ymin:ymax]:
for pix in j[xmin:xmax]:
# calcul de la moyenne autour du pixel
if pix != pixelMap[y][x]:
# si le pixel n'est pas le pixel courant (mais ceux autour)
if pix != pixelMap[y][x]:
# calcul de la moyenne autour du pixel
rMoy += pix.r;
gMoy += pix.g;
bMoy += pix.b;
count += 1
# si il y a au moins un pixel autour (normalement tjs mais évite l'erreur div par zéro)
if count > 0:
# on calcule les moyennes somme(xi) / n
rMoy = int( rMoy / count )
gMoy = int( gMoy / count )
bMoy = int( bMoy / count )
# calcul de la différence entre les couleurs du pixel et la moyenne des couleurs des pixels autour
rInterval = abs( pixelMap[y][x].r - rMoy )
gInterval = abs( pixelMap[y][x].g - gMoy )
bInterval = abs( pixelMap[y][x].b - bMoy )
# calcul de la différence en nuance de gris (moyenne des couleurs)
rgbInterval = ( rInterval + gInterval + bInterval ) / 3
# si la couleur est trop "différente" alors on remplace sa couleur par la moyenne des couleurs alentours
# si la couleur est trop "différente" (dépend du seuil) alors on remplace sa couleur par la moyenne des couleurs alentours
if rgbInterval > seuil:
pixelMap[y][x].setRGB(rMoy, gMoy, bMoy);

View File

@ -12,7 +12,69 @@ import random
import sys
import time
# arrêt si moins de 2 arguments
if len(sys.argv) < 3:
print "Require 2 args : \n* input image\n* output image"
exit()
################" INTERFACE "###################
print "+-------------------------+"
print "| TRAITEMENT D'IMAGE |"
print "+-------------------------+"
print "| <in> %s |" % exactLength( sys.argv[1], 17, -1)
print "| <out> %s |" % exactLength( sys.argv[2], 17, -1)
print "+-------------------------+"
print "| %s |" % exactLength("TESTS DE FICHIER", 23, 0)
print "| %s |" % exactLength("", 23, 0)
print "| 0) %s |" % exactLength("Creation manuelle", 20, -1)
print "| 1) %s |" % exactLength("Parse/Unparse", 20, -1)
print "| 2) %s |" % exactLength("Afficher palette", 20, -1)
print "+-------------------------+"
print "| %s |" % exactLength("TESTS DE BRUIT", 23, 0)
print "| %s |" % exactLength("", 23, 0)
print "| 3) %s |" % exactLength("Salt&Pepper", 20, -1)
print "| 4) %s |" % exactLength("Additif", 20, -1)
print "+-------------------------+"
print "| %s |" % exactLength("TESTS DE DIFFERENCES", 23, 0)
print "| %s |" % exactLength("", 23, 0)
print "| 5) %s |" % exactLength("Difference en %", 20, -1)
print "| 6) %s |" % exactLength("Difference en image", 20, -1)
print "| 7) %s |" % exactLength("Fusion d'images", 20, -1)
print "+-------------------------+"
print
while True:
action = int( raw_input("choix: ") )
if action >= 0 and action <= 7:
break;
print
print "+-------------------------+---------+"
execTime = Timer(); execTime.reset();
if action == 0:
testManualCreation() # teste la création d'un fichier à partir d'une matrice uniquement
elif action == 1:
testFileIntegrity() # teste le PARSE/UNPARSE
elif action == 2:
printIntPalette() # affiche la palette d'une image
elif action == 3:
testSaltAndPepper() # teste le bruitage/débruitage de type "Sel & Poivre"
elif action == 4:
testAdditiveNoise() # teste le bruitage/débruitage de type "Additif"
elif action == 5:
printImageQuality() # compare 2 images et donne le pourcentage de ressemblance/différence
elif action == 6:
imageForImageQuality() # crée une image correspondant aux différences de 2 images
elif action == 7:
mergeImages() # crée une image étant la fusion (addition) de 2 images
else:
print "Error! aborting"
print "+-------------------------+---------+"
print "| EXECUTION TIME | %s |" % execTime.get()
print "+-------------------------+---------+"
############ TESTS ############
@ -23,7 +85,7 @@ import time
# testSaltAndPepper() # teste le bruitage/débruitage de type "Sel & Poivre"
# testAdditiveNoise() # teste le bruitage/débruitage de type "Additif"
printImageQuality() # compare 2 images et donne le pourcentage de ressemblance/différence
# printImageQuality() # compare 2 images et donne le pourcentage de ressemblance/différence
# imageForImageQuality() # crée une image correspondant aux différences de 2 images
# mergeImages() # crée une image étant la fusion (addition) de 2 images

View File

@ -13,13 +13,6 @@ import sys
import time
# arrêt si moins de 2 arguments
if len(sys.argv) < 3:
print "Require 2 args : \n* input image\n* output image"
exit()
@ -39,7 +32,44 @@ class Timer:
# affiche la valeur du chrono
def get(self):
return float(int(100*(time.time()-self.timer)))/100
return exactLength( str(float(int(100*(time.time()-self.timer)))/100), 7, 0 )
# retourne la chaine complétée d'espaces pour arriver à la taille length #
##########################################################################
# @param text le texte de base
# @param length la taille totale à renvoyer
# @param position position du texte ( <0 = gauche ; 0 = centre ; >0 = droite )
#
# @exception si le texte est plus grand que la position on renvoie le texte sans exception
def exactLength(text, length, position=0):
# si texte aussi long ou plus long que la taille, on renvoie le text
if len(text) >= length:
return text;
# initialisation de la variable qui sera retournée
string = ""
# texte à gauche
if position < 0:
return text + " "*(length-len(text))
# texte à droite
elif position > 0:
return " "*(length-len(text)) + text
# texte au centre
else:
return " "*( (length-len(text))//2 ) + text + " "*( length - (length-len(text))//2 - len(text) )
@ -59,14 +89,13 @@ class Timer:
def testFileIntegrity():
t = Timer();
total = Timer(); total.reset();
# lecture du fichier
print "Reading Image -",; t.reset();
with open( sys.argv[1] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
img = BMPFile(); # Instanciation du BMPFile
@ -75,7 +104,7 @@ def testFileIntegrity():
# Parsing
print "Parsing file -",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
img.header.info();
@ -86,23 +115,23 @@ def testFileIntegrity():
# Unparsing
print "Unparsing file -",; t.reset();
img.unparse();
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# Writing
print "Writing file -",; t.reset();
img.write( sys.argv[2] )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# lecture du fichier
print "Reading Image -",; t.reset();
with open( sys.argv[2] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# Parsing
print "Parsing file -",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
img.header.info();
@ -125,14 +154,13 @@ def testFileIntegrity():
def testSaltAndPepper():
t = Timer();
total = Timer(); total.reset();
# lecture du fichier
print "Reading Image -",; t.reset();
print "| Reading Image |",; t.reset();
with open( sys.argv[1] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
img = BMPFile(); # Instanciation du BMPFile
@ -140,44 +168,44 @@ def testSaltAndPepper():
# Parsing
print "Parsing file -",; t.reset();
print "| Parsing file |",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Creating Salt&Pepper -",; t.reset();
noise.SaltAndPepper_set(img.content.map, seuil=10)
print "Done in %s s" % (t.get())
print "| Creating Salt&Pepper |",; t.reset();
noise.SaltAndPepper_set(img.content.map, seuil=50)
print "%s |" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
print "| Unparsing file |",; t.reset();
img.unparse()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
print "| Writing file |",; t.reset();
img.write( "SaltAndPepper.bmp" )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Removing Salt&Pepper -",; t.reset();
noise.SaltAndPepper_unset(img.content.map)
print "Done in %s s" % (t.get())
print "| Removing Salt&Pepper |",; t.reset();
noise.SaltAndPepper_unset(img.content.map, seuil=1, borne=1)
print "%s |" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
print "| Unparsing file |",; t.reset();
img.unparse()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
print "| Writing file |",; t.reset();
img.write( sys.argv[2] )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "\nExecution Time: %s seconds" % total.get()
@ -199,14 +227,13 @@ def testSaltAndPepper():
def testAdditiveNoise():
t = Timer();
total = Timer(); total.reset();
# lecture du fichier
print "Reading Image -",; t.reset();
print "| Reading Image |",; t.reset();
with open( sys.argv[1] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
img = BMPFile(); # Instanciation du BMPFile
@ -214,44 +241,44 @@ def testAdditiveNoise():
# Parsing
print "Parsing file -",; t.reset();
print "| Parsing file |",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Creating Additive -",; t.reset();
print "| Creating Additive |",; t.reset();
noise.AdditiveNoise_set(img.content.map, seuil=50)
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
print "| Unparsing file |",; t.reset();
img.unparse()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
print "| Writing file |",; t.reset();
img.write( "AdditiveNoise.bmp" )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Removing Additive -",; t.reset();
print "| Removing Additive |",; t.reset();
noise.AdditiveNoise_unset(img.content.map)
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
print "| Unparsing file |",; t.reset();
img.unparse()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
print "| Writing file |",; t.reset();
img.write( sys.argv[2] )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "\nExecution Time: %s seconds" % total.get()
@ -285,7 +312,7 @@ def testManualCreation():
img.unparse();
img.write( sys.argv[1] )
img.write( sys.argv[2] )
# print img.binData
@ -334,23 +361,22 @@ def printIntPalette():
# Affiche le pourcentage de ressemblance/différence
def printImageQuality():
t = Timer();
total = Timer(); total.reset();
imageFile, modelFile = "", ""
# lecture des fichiers
print "Reading files -",; t.reset();
print "| Reading files |",; t.reset();
with open( sys.argv[1] ) as f:
imageFile = f.read();
with open( sys.argv[2] ) as f:
modelFile = f.read();
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# parsage
print "Parsing images -",; t.reset();
print "| Parsing images |",; t.reset();
image = BMPFile(); image.parse( imageFile );
model = BMPFile(); model.parse( modelFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# condition
imagePixelCount = image.header.width * image.header.height
@ -362,7 +388,7 @@ def printImageQuality():
# comparaison
print "Comparaison -",; t.reset();
print "| Comparaison |",; t.reset();
count, totalCount = [0,0,0], imagePixelCount*256*3
for y in range(0, image.header.height):
for x in range(0, image.header.width):
@ -373,12 +399,10 @@ def printImageQuality():
differenceCount = count[0] + count[1] + count[2]
percentage = 100.0 * (totalCount-differenceCount) / totalCount
percentage = int(100*percentage)/100.0
print "Done in %s s" % (t.get())
print
print "Qualité = %s %s" % (percentage, "%")
print "Différence = %s %s" % (100-percentage, "%")
print "\nExecution Time: %s seconds" % total.get()
print "%s |" % (t.get())
print "+-------------------------+"
print "| Commun = %s |" % exactLength( str(percentage)+"%", 10, -1 );
print "| Difference = %s |" % exactLength( str(100-percentage)+"%", 10, -1 );
@ -396,7 +420,6 @@ def printImageQuality():
# Unparse cette matrice et l'enregistre dans le fichier "compare.bmp"
def imageForImageQuality():
t = Timer();
total = Timer(); total.reset();
imageFile, modelFile = "", ""
image, model, newImg = BMPFile(), BMPFile(), BMPFile()
@ -407,13 +430,13 @@ def imageForImageQuality():
imageFile = f.read();
with open( sys.argv[2] ) as f:
modelFile = f.read();
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# parsage
print "Parsing images -",; t.reset();
image.parse( imageFile );
model.parse( modelFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# condition
imagePixelCount = image.header.width * image.header.height
@ -439,15 +462,15 @@ def imageForImageQuality():
print "Unparsing -",; t.reset();
newImg.unparse();
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Writing File -",; t.reset();
with open("compare.bmp", "w") as f:
f.write( newImg.binData );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "\nExecution Time: %s seconds" % total.get()
@ -467,7 +490,6 @@ def imageForImageQuality():
# Unparse le tout et l'enregistre dans merge.bmp
def mergeImages():
t = Timer();
total = Timer(); total.reset();
imageFile, modelFile = "", ""
image, model, newImg = BMPFile(), BMPFile(), BMPFile()
@ -478,13 +500,13 @@ def mergeImages():
imageFile = f.read();
with open( sys.argv[2] ) as f:
modelFile = f.read();
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# parsage
print "Parsing images -",; t.reset();
image.parse( imageFile );
model.parse( modelFile );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
# condition
@ -507,19 +529,19 @@ def mergeImages():
( image.content.map[y][x].b + model.content.map[y][x].b ) % 256
) )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Unparsing -",; t.reset();
newImg.unparse(newBpp=24);
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "Writing File -",; t.reset();
with open("merge.bmp", "w") as f:
f.write( newImg.binData );
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "\nExecution Time: %s seconds" % total.get()
@ -544,14 +566,13 @@ def mergeImages():
def calSaltAndPepper():
t = Timer();
total = Timer(); total.reset();
# lecture du fichier
print "Reading Image -",; t.reset();
with open( sys.argv[1] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
img = BMPFile(); # Instanciation du BMPFile
@ -567,10 +588,10 @@ def calSaltAndPepper():
noise.SaltAndPepper_unset(img.content.map, seuil=seuil, borne=borne)
img.unparse(newBpp=8)
img.write( "SaltAndPepper/%s_%s.bmp" % (seuil, borne) )
print "Done in %s s" % (t.get())
print "%s |" % (t.get())
print "\nExecution Time: %s seconds" % total.get()