saltandpepepr

This commit is contained in:
xdrm-brackets 2015-09-14 16:59:35 +02:00
parent c14eecfdee
commit bee34b1ea8
4 changed files with 92 additions and 47 deletions

View File

@ -9,7 +9,6 @@ class BMPHeader:
def __init__(self): def __init__(self):
self.binData = 0; # header brut (format initial: bin) self.binData = 0; # header brut (format initial: bin)
self.intData = 0; # header brut (format entier) self.intData = 0; # header brut (format entier)
self.hexData = 0; # header brut (format hexadécimal)
self.signature = 0; # signature (4D42) self.signature = 0; # signature (4D42)
self.fileSize = 0; # taille du fichier bmp (bytes) self.fileSize = 0; # taille du fichier bmp (bytes)
@ -212,7 +211,7 @@ class BMPContent:
# unparse une map de pixels en binaire # unparse une map de pixels en binaire
def unparse(self, map, headerHandler=None): def unparse(self, map, headerHandler=None, newBpp=None):
self.map = map self.map = map
if not isinstance(headerHandler, BMPHeader): if not isinstance(headerHandler, BMPHeader):
@ -240,9 +239,13 @@ class BMPContent:
headerHandler.rowSize = headerHandler.size / headerHandler.height # taille réelle de ligne headerHandler.rowSize = headerHandler.size / headerHandler.height # taille réelle de ligne
headerHandler.fileSize = headerHandler.offset + headerHandler.size # taille du fichier BMP = offset + taille map 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 = "" self.binData = ""
for line in self.map[::-1]: for line in self.map[::-1]:
for pixel in line: for pixel in line:
pixel.setRGB(pixel.r, pixel.g, pixel.b, bpp=headerHandler.bpp);
self.binData += pixel.binData self.binData += pixel.binData
for zero in range(0, headerHandler.padding): for zero in range(0, headerHandler.padding):
self.binData += chr(0) self.binData += chr(0)
@ -395,7 +398,7 @@ class BMPFile:
self.intPalette.append( ord(byte) ) self.intPalette.append( ord(byte) )
# unparse à partir d'un <BMPHeader> et d'un <BMPContent> # unparse à partir d'un <BMPHeader> et d'un <BMPContent>
def unparse(self): def unparse(self, newBpp=None):
# on définit la palette par défaut # 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] 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]
@ -403,8 +406,12 @@ class BMPFile:
for byte in self.intPalette: for byte in self.intPalette:
self.binPalette += chr(byte) self.binPalette += chr(byte)
bpp = None
if newBpp in [1,4,8,24]: # si nouveau bpp défini
bpp = newBpp;
# on déparse les classes utilisées # on déparse les classes utilisées
self.content.unparse( self.content.map, self.header ) self.content.unparse( self.content.map, self.header, newBpp=bpp )
self.header.unparse() self.header.unparse()
# on enregistre le contenu brut binaire du fichier complet # on enregistre le contenu brut binaire du fichier complet

View File

@ -1,8 +1,39 @@
# ~*~ encoding: utf-8 ~*~ # # ~*~ encoding: utf-8 ~*~ #
import random
class Noise: class Noise:
def getShape(self, coords, pixelMap, pixelList ): # return PixelList
def SaltAndPepper_set(self, seuil, pixelMap):
seuil = float(seuil);
while seuil >= 1:
seuil /= 100
nbPixel = int( len(pixelMap) * len(pixelMap[0]) * seuil )
for bruit in range(0, nbPixel ):
x = random.randint(0, len(pixelMap[0]) - 1 )
y = random.randint(0, len(pixelMap) - 1 )
if random.randint(0,1) == 1:
pixelMap[y][x].setRGB(255,255,255);
else:
pixelMap[y][x].setRGB(0,0,0);
def getShapeRecursive(self, coords, pixelMap, pixelList ): # return PixelList
# coords = [lastx, lasty, x, y] # coords = [lastx, lasty, x, y]
width = len( pixelMap[0] ) width = len( pixelMap[0] )
height = len( pixelMap ) height = len( pixelMap )
@ -49,3 +80,47 @@ class Noise:
self.getShape( [x, y, x, y-1], pixelMap, pixelList) # 8 self.getShape( [x, y, x, y-1], pixelMap, pixelList) # 8
self.getShape( [x, y, x+1, y-1], pixelMap, pixelList) # 9 self.getShape( [x, y, x+1, y-1], pixelMap, pixelList) # 9
def getShape(self, coords, pixelMap, pixelList):
# coords = [lastx, lasty, x, y]
width = len( pixelMap[0] )
height = len( pixelMap )
lastx = coords[0]
lasty = coords[1]
x = coords[2]
y = coords[3]
if x<width and x>=0 and y<height and y>=0:
already = False;
for pix in pixelList:
if pix == pixelMap[y][x]:
already = True;
break
# si le pixel n'est pas déjà dans le tableau
if not already:
# si trop de différence
lastP = pixelMap[lasty][lastx]
pix = pixelMap[y][x]
if abs(lastP.r-pix.r) <= 50 and abs(lastP.g-pix.g) <= 50 and abs(lastP.b-pix.b) <= 50:
return pixelMap[y][x];
# self.getShape( [x, y, x-1, y+1], pixelMap, pixelList) # 1
# self.getShape( [x, y, x, y+1], pixelMap, pixelList) # 2
# self.getShape( [x, y, x+1, y+1], pixelMap, pixelList) # 3
# self.getShape( [x, y, x-1, y ], pixelMap, pixelList) # 4
# # current pixel
# self.getShape( [x, y, x+1, y ], pixelMap, pixelList) # 6
# self.getShape( [x, y, x-1, y-1], pixelMap, pixelList) # 7
# self.getShape( [x, y, x, y-1], pixelMap, pixelList) # 8
# self.getShape( [x, y, x+1, y-1], pixelMap, pixelList) # 9
return None # return none si le pixel n'est plus de la forme

View File

@ -26,54 +26,17 @@ def testFileIntegrity():
# Instanciation du BMPFile # Instanciation du BMPFile
img = BMPFile() img = BMPFile()
# Instanciation du NoiseObject
noise = Noise();
# Parsing # Parsing
img.parse( binFile ); img.parse( binFile );
# détection des formes noise.SaltAndPepper_set(10, img.content.map)
noise = Noise();
pixelsDone = []
for y in range(0,len(img.content.map)):
for x in range(0, len(img.content.map[0])):
# on traite si on a pas déjà
already = False
for i in pixelsDone:
if i == img.content.map[y][x]:
already = True
break
if not already:
pixelList = []
randomRGB = [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]
noise.getShape([x, y, x, y], img.content.map, pixelList)
for pixel in pixelList:
pixel.setRGB(randomRGB[0], randomRGB[1], randomRGB[2]);
pixelsDone = pixelsDone + pixelList;
#inct = 50; # incertitude
# MODIFICATIONS des pixels
#for line in img.content.map:
# for pixel in line:
# pixel.setRGB(
# (230-25) + (2*25*pixel.r/256), # 230 ± 25
# (170-inct) + (2*inct*pixel.g/256), # 170 ± 50
# (100-inct) + (2*inct*pixel.b/256), # 100 ± 50
# bpp=24
# )
# Unparsing # Unparsing
img.unparse() img.unparse(newBpp=24)
print img.binData print img.binData

Binary file not shown.

Before

Width:  |  Height:  |  Size: 443 B

After

Width:  |  Height:  |  Size: 768 KiB