BMPFile fonctionne avec 8 et 24 et peut se ré-utiliser. Noise fonctionne, création des calibrages => SaltAndPepper/
This commit is contained in:
parent
2e6482055a
commit
1a281d4f1d
|
@ -66,6 +66,8 @@ class BMPHeader:
|
|||
for byte in parsingData:
|
||||
self.intData.append( ord(byte) )
|
||||
|
||||
#self.info()
|
||||
|
||||
|
||||
|
||||
# fonction qui créer <self.binData> à partir des attributs
|
||||
|
@ -91,8 +93,8 @@ class BMPHeader:
|
|||
self.fromInt( self.colorINb, 4) # nombre de couleurs importantes de l'image (ou 0)
|
||||
|
||||
# calculated values
|
||||
self.rowSize = self.size / self.height # taille réelle d'une ligne (+padding)
|
||||
self.padding = self.rowSize - self.width*self.bpp/8 # bourrage (nb de bytes)
|
||||
# self.rowSize = self.size / self.height # taille réelle d'une ligne (+padding)
|
||||
# self.padding = self.rowSize - self.width*self.bpp/8 # bourrage (nb de bytes)
|
||||
|
||||
|
||||
self.binData = ""
|
||||
|
@ -211,25 +213,25 @@ class BMPContent:
|
|||
|
||||
|
||||
# unparse une map de pixels en binaire
|
||||
def unparse(self, map, headerHandler=None, newBpp=None):
|
||||
def unparse(self, map, palettes, headerHandler=None, newBpp=None):
|
||||
self.map = map
|
||||
|
||||
if not isinstance(headerHandler, BMPHeader):
|
||||
headerHandler = BMPHeader()
|
||||
|
||||
|
||||
bpp = self.map[0][0].bpp;
|
||||
|
||||
headerHandler.signature = int( 0x4D42 )
|
||||
headerHandler.offset = 54 + 68 # taille header(54) + taille palette(68)
|
||||
headerHandler.infoSize = headerHandler.offset - 14 # valeur d'offset - 14
|
||||
headerHandler.offset = 54 + len(palettes[bpp]) # taille header(54) + taille palette(68)
|
||||
headerHandler.infoSize = 40 # valeur d'offset - 14
|
||||
headerHandler.width = len( map[0] ) # récupérée à partir de l'argument <map>
|
||||
headerHandler.height = len( map ) # récupérée à partir de l'argument <map>
|
||||
headerHandler.plans = 1
|
||||
# on récupère l'attribut BPP des RGBPixel
|
||||
headerHandler.bpp = self.map[0][0].bpp
|
||||
headerHandler.bpp = map[0][0].bpp
|
||||
headerHandler.compType = 0
|
||||
headerHandler.horiRes = int( 0xB13 )
|
||||
headerHandler.vertRes = int( 0xB13 )
|
||||
headerHandler.horiRes = 0
|
||||
headerHandler.vertRes = 0
|
||||
headerHandler.colorNb = 0
|
||||
headerHandler.colorINb = 0
|
||||
|
||||
|
@ -237,15 +239,16 @@ class BMPContent:
|
|||
headerHandler.bpp = newBpp;
|
||||
|
||||
# valeurs calculées
|
||||
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.rowSize = headerHandler.width * headerHandler.bpp/8
|
||||
headerHandler.padding = 4 - headerHandler.rowSize%4
|
||||
if headerHandler.padding == 4:
|
||||
headerHandler.padding = 0;
|
||||
headerHandler.rowSize += headerHandler.padding
|
||||
headerHandler.size = headerHandler.rowSize * headerHandler.height # taille de la map (hauteur*largeur* nombre d'octets par pixel)
|
||||
headerHandler.fileSize = headerHandler.offset + headerHandler.size # taille du fichier BMP = offset + taille map
|
||||
|
||||
|
||||
|
||||
self.binData = ""
|
||||
for line in self.map[::-1]:
|
||||
for line in map[::-1]:
|
||||
for pixel in line:
|
||||
pixel.setRGB(pixel.r, pixel.g, pixel.b, bpp=headerHandler.bpp);
|
||||
self.binData += pixel.binData
|
||||
|
@ -401,12 +404,15 @@ class BMPFile:
|
|||
|
||||
# unparse à partir d'un <BMPHeader> et d'un <BMPContent>
|
||||
def unparse(self, newBpp=None):
|
||||
# on définit la palette par défaut
|
||||
self.intPalette = [66, 71, 82, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
|
||||
palette = [[]]*25
|
||||
|
||||
# définition des palettes par défaut
|
||||
tmp = [[x,x,x,0] for x in range(0,256)]
|
||||
palette[8] = []; palette[8] += [x for l in tmp for x in l]
|
||||
|
||||
palette[24] = [66, 71, 82, 115] + [0]*48 + [2] + [0]*15
|
||||
|
||||
self.binPalette = ""
|
||||
for byte in self.intPalette:
|
||||
self.binPalette += chr(byte)
|
||||
|
||||
bpp = None
|
||||
if newBpp in [1,4,8,24]: # si nouveau bpp défini
|
||||
|
@ -414,9 +420,15 @@ class BMPFile:
|
|||
|
||||
|
||||
# on déparse les classes utilisées
|
||||
self.content.unparse( self.content.map, self.header, newBpp=bpp )
|
||||
self.content.unparse( self.content.map, palette, self.header, newBpp=bpp )
|
||||
self.header.unparse()
|
||||
|
||||
self.intPalette = palette[self.header.bpp]
|
||||
|
||||
self.binPalette = ""
|
||||
for byte in self.intPalette:
|
||||
self.binPalette += chr(byte)
|
||||
|
||||
# on enregistre le contenu brut binaire du fichier complet
|
||||
self.binData = "%s%s%s" % (self.header.binData, self.binPalette, self.content.binData)
|
||||
|
||||
|
|
|
@ -29,13 +29,14 @@ class Noise:
|
|||
width = len( pixelMap[0] )
|
||||
height = len( pixelMap )
|
||||
|
||||
seuil = int( .01 * 256 );
|
||||
borne = 50
|
||||
seuil = int( .1 * 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:
|
||||
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
|
||||
|
|
234
code/bmp.py
234
code/bmp.py
|
@ -21,8 +21,6 @@ class Timer:
|
|||
def get(self):
|
||||
return float(int(100*(time.time()-self.timer)))/100
|
||||
|
||||
|
||||
|
||||
def testFileIntegrity():
|
||||
|
||||
t = Timer();
|
||||
|
@ -36,6 +34,60 @@ def testFileIntegrity():
|
|||
print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
img = BMPFile(); # Instanciation du BMPFile
|
||||
|
||||
|
||||
# Parsing
|
||||
print "Parsing file -",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
img.header.info();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Unparsing
|
||||
print "Unparsing file -",; t.reset();
|
||||
img.unparse();
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
# Writing
|
||||
print "Writing file -",; t.reset();
|
||||
img.write( sys.argv[2] )
|
||||
print "Done in %s 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())
|
||||
|
||||
# Parsing
|
||||
print "Parsing file -",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
img.header.info();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def testSaltAndPepper():
|
||||
|
||||
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
|
||||
|
||||
|
@ -47,85 +99,57 @@ def testFileIntegrity():
|
|||
|
||||
|
||||
|
||||
# print "Creating Salt&Pepper -",; t.reset();
|
||||
# noise.SaltAndPepper_set(10, 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())
|
||||
|
||||
# # image to stdout
|
||||
# print "Writing file -",; t.reset();
|
||||
# img.write( "SaltAndPepper.bmp" )
|
||||
# print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
|
||||
|
||||
# print "Removing Salt&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())
|
||||
|
||||
# # image to stdout
|
||||
# print "Writing file -",; t.reset();
|
||||
# img.write( sys.argv[2] )
|
||||
# print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
|
||||
|
||||
print "Getting Shape -",; t.reset();
|
||||
|
||||
pixelList = [];
|
||||
|
||||
try:
|
||||
noise.getShapeRecursive([0,0,0,0], img.content.map, pixelList);
|
||||
except RuntimeError as e:
|
||||
noise.getShapeRecursive([0,0,0,0], img.content.map, pixelList);
|
||||
|
||||
|
||||
|
||||
print "Creating Salt&Pepper -",; t.reset();
|
||||
noise.SaltAndPepper_set(50, img.content.map)
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
print "Unparsing file -",; t.reset();
|
||||
img.unparse(newBpp=24)
|
||||
img.unparse(newBpp=8)
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
# image to stdout
|
||||
print "Writing file -",; t.reset();
|
||||
img.write( "shape.bmp" )
|
||||
img.write( "SaltAndPepper.bmp" )
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
|
||||
|
||||
print "Removing Salt&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=8)
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 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()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def printIntPalette():
|
||||
img = BMPFile();
|
||||
|
||||
# lecture du fichier
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
|
||||
img.parse(binFile);
|
||||
|
||||
print img.intPalette;
|
||||
|
||||
|
||||
|
||||
|
||||
def testManualCreation():
|
||||
img = BMPFile()
|
||||
for y in range(0, 100):
|
||||
|
@ -157,6 +181,92 @@ if len(sys.argv) < 3:
|
|||
|
||||
|
||||
|
||||
# MAIN
|
||||
|
||||
############ TESTS ############
|
||||
#testManualCreation()
|
||||
testFileIntegrity()
|
||||
#testSaltAndPepper()
|
||||
#testFileIntegrity()
|
||||
#printIntPalette();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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())
|
||||
|
||||
|
||||
img = BMPFile(); # Instanciation du BMPFile
|
||||
noise = Noise(); # Instanciation du NoiseObject
|
||||
|
||||
|
||||
# Parsing
|
||||
print "Parsing file -",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
|
||||
print "Creating Salt&Pepper -",; t.reset();
|
||||
noise.SaltAndPepper_set(50, img.content.map)
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
print "Unparsing file -",; t.reset();
|
||||
img.unparse(newBpp=8)
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
# image to stdout
|
||||
print "Writing file -",; t.reset();
|
||||
img.write( "SaltAndPepper.bmp" )
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
|
||||
|
||||
|
||||
print "Removing Salt&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=8)
|
||||
print "Done in %s s" % (t.get())
|
||||
|
||||
# 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()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
############ CALIBRATE ############
|
||||
calSaltAndPepper()
|
Loading…
Reference in New Issue