Poivre&Sel set, unset partiel (wtf?) et modification de l'affichage

This commit is contained in:
xdrm-brackets 2015-09-14 21:58:21 +02:00
parent bee34b1ea8
commit 999f8580ae
8 changed files with 101 additions and 10 deletions

View File

@ -233,14 +233,16 @@ class BMPContent:
headerHandler.colorNb = 0
headerHandler.colorINb = 0
if newBpp in [1,4,8,24]: # si nouveau bpp défini
headerHandler.bpp = newBpp;
# valeurs calculées
headerHandler.padding = ( 4 - headerHandler.width*3 % 4 ) % 4
headerHandler.size = headerHandler.width * headerHandler.height * headerHandler.bpp # taille de la map (hauteur*largeur* nombre d'octets par pixel)
headerHandler.padding = ( 4 - headerHandler.width*headerHandler.bpp/3 % 4 ) % 4
headerHandler.size = headerHandler.width * headerHandler.height * headerHandler.bpp # taille de la map (hauteur*largeur* nombre d'octets par pixel)
headerHandler.rowSize = headerHandler.size / headerHandler.height # taille réelle de ligne
headerHandler.fileSize = headerHandler.offset + headerHandler.size # taille du fichier BMP = offset + taille map
if newBpp in [1,4,8,24]: # si nouveau bpp défini
headerHandler.bpp = newBpp;
self.binData = ""
for line in self.map[::-1]:
@ -410,6 +412,7 @@ class BMPFile:
if newBpp in [1,4,8,24]: # si nouveau bpp défini
bpp = newBpp;
# on déparse les classes utilisées
self.content.unparse( self.content.map, self.header, newBpp=bpp )
self.header.unparse()
@ -419,4 +422,10 @@ class BMPFile:
self.intData = []
for byte in self.binData:
self.intData.append( ord(byte) )
self.intData.append( ord(byte) )
# écrit l'image dans un fichier
def write(self, filename):
with open(filename,"w") as file:
file.write( self.binData );

View File

@ -5,6 +5,7 @@ import random
class Noise:
# ajout de bruit "poivre & sel" avec un seuil (% de l'image bruité)
def SaltAndPepper_set(self, seuil, pixelMap):
seuil = float(seuil);
@ -23,9 +24,56 @@ class Noise:
pixelMap[y][x].setRGB(0,0,0);
# enlève le bruit "poivre et sel"
def SaltAndPepper_unset(self, pixelMap):
width = len( pixelMap[0] )
height = len( pixelMap )
seuil = int( .5 * 256 );
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 >= 235 or pMoy <= 20:
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
if y-1 > -1:
ymin = y-1
if y+1 < height:
ymax = y+1
if x-1 > -1:
xmin = x-1
if x+1 < width:
xmax = x+1
for j in range(0, ymax-xmin): # on parcourt les pixels autour
for i in range(0, xmax-xmin):
# calcul de la moyenne autour du pixel
if i != x and j != y:
rMoy += pixelMap[j][i].r;
gMoy += pixelMap[j][i].g;
bMoy += pixelMap[j][i].b;
count += 1
if count > 0:
rMoy = int( rMoy / count )
gMoy = int( gMoy / count )
bMoy = int( bMoy / count )
rInterval = abs( pixelMap[y][x].r - rMoy )
gInterval = abs( pixelMap[y][x].g - gMoy )
bInterval = abs( pixelMap[y][x].b - bMoy )
rgbInterval = ( rInterval + gInterval + bInterval ) / 3
# si la couleur est trop "différente" alors on remplace sa couleur par la moyenne des couleurs alentours
if rgbInterval > seuil:
pixelMap[y][x].setRGB(rMoy, gMoy, bMoy);

View File

View File

@ -0,0 +1 @@
Mauvais format (erreur de taille)

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

View File

@ -8,38 +8,71 @@ from BMPFile import *
from Noise import *
import random
import sys
import time
if len(sys.argv) < 3:
print "Require 2 args : \n* input image\n* output image"
exit()
class Timer:
def __init__(self):
self.timer = time.time();
def reset(self):
self.timer = time.time();
def get(self):
return float(int(100*(time.time()-self.timer)))/100
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())
img = BMPFile(); # Instanciation du BMPFile
noise = Noise(); # Instanciation du NoiseObject
# Instanciation du BMPFile
img = BMPFile()
# Instanciation du NoiseObject
noise = Noise();
# Parsing
print "Parsing file -",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "Creating random extreme pixels -",; t.reset();
noise.SaltAndPepper_set(10, img.content.map)
print "Done in %s s" % (t.get())
print "Removing salt and pepper -",; t.reset();
noise.SaltAndPepper_unset(img.content.map)
print "Done in %s s" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
img.unparse(newBpp=24)
print "Done in %s s" % (t.get())
print img.binData
# image to stdout
print "Writing file -",; t.reset();
img.write( sys.argv[2] )
print "Done in %s s" % (t.get())
print "\nExecution Time: %s seconds" % total.get()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 768 KiB

After

Width:  |  Height:  |  Size: 768 KiB