2015-09-06 12:54:58 +00:00
|
|
|
# ~*~ encoding: utf-8 ~*~ #
|
|
|
|
###########################
|
|
|
|
# TRAITEMENT D'IMAGES #
|
|
|
|
###########################
|
|
|
|
|
2015-09-06 14:01:17 +00:00
|
|
|
# classes
|
2015-09-09 13:52:20 +00:00
|
|
|
from BMPFile import *
|
2015-09-10 21:37:17 +00:00
|
|
|
from Noise import *
|
2015-09-08 20:42:28 +00:00
|
|
|
import random
|
2015-09-06 12:54:58 +00:00
|
|
|
import sys
|
2015-09-14 19:58:21 +00:00
|
|
|
import time
|
2015-09-08 19:17:00 +00:00
|
|
|
|
2015-09-14 19:58:21 +00:00
|
|
|
class Timer:
|
|
|
|
def __init__(self):
|
|
|
|
self.timer = time.time();
|
2015-09-09 20:12:37 +00:00
|
|
|
|
2015-09-14 19:58:21 +00:00
|
|
|
def reset(self):
|
|
|
|
self.timer = time.time();
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
return float(int(100*(time.time()-self.timer)))/100
|
2015-09-09 20:12:37 +00:00
|
|
|
|
2015-09-08 20:42:28 +00:00
|
|
|
def testFileIntegrity():
|
2015-09-14 19:58:21 +00:00
|
|
|
|
|
|
|
t = Timer();
|
|
|
|
total = Timer(); total.reset();
|
|
|
|
|
|
|
|
|
2015-09-08 20:42:28 +00:00
|
|
|
# lecture du fichier
|
2015-09-16 13:37:19 +00:00
|
|
|
print "Reading Image -",; t.reset();
|
2015-09-08 20:42:28 +00:00
|
|
|
with open( sys.argv[1] ) as file:
|
|
|
|
binFile = file.read()
|
2015-09-14 19:58:21 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
|
|
|
|
|
|
|
img = BMPFile(); # Instanciation du BMPFile
|
2015-09-08 20:42:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Parsing
|
2015-09-16 13:37:19 +00:00
|
|
|
print "Parsing file -",; t.reset();
|
2015-09-08 20:42:28 +00:00
|
|
|
img.parse( binFile );
|
2015-09-14 19:58:21 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-08 20:42:28 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
img.header.info();
|
2015-09-08 20:42:28 +00:00
|
|
|
|
|
|
|
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# Unparsing
|
|
|
|
print "Unparsing file -",; t.reset();
|
|
|
|
img.unparse();
|
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# Writing
|
|
|
|
print "Writing file -",; t.reset();
|
|
|
|
img.write( sys.argv[2] )
|
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# 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())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# Parsing
|
|
|
|
print "Parsing file -",; t.reset();
|
|
|
|
img.parse( binFile );
|
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
|
|
|
img.header.info();
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
def testSaltAndPepper():
|
|
|
|
|
|
|
|
t = Timer();
|
|
|
|
total = Timer(); total.reset();
|
|
|
|
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# 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
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# Parsing
|
|
|
|
print "Parsing file -",; t.reset();
|
|
|
|
img.parse( binFile );
|
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
print "Creating Salt&Pepper -",; t.reset();
|
2015-09-17 13:55:16 +00:00
|
|
|
noise.SaltAndPepper_set(img.content.map, seuil=10)
|
2015-09-14 19:58:21 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
2015-09-09 20:37:44 +00:00
|
|
|
# Unparsing
|
2015-09-16 13:37:19 +00:00
|
|
|
print "Unparsing file -",; t.reset();
|
2015-09-17 10:01:29 +00:00
|
|
|
img.unparse()
|
2015-09-14 19:58:21 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-08 20:42:28 +00:00
|
|
|
|
2015-09-14 19:58:21 +00:00
|
|
|
# image to stdout
|
2015-09-16 13:37:19 +00:00
|
|
|
print "Writing file -",; t.reset();
|
2015-09-16 19:14:08 +00:00
|
|
|
img.write( "SaltAndPepper.bmp" )
|
2015-09-14 19:58:21 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-08 19:17:00 +00:00
|
|
|
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
print "Removing Salt&Pepper -",; t.reset();
|
|
|
|
noise.SaltAndPepper_unset(img.content.map)
|
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# Unparsing
|
|
|
|
print "Unparsing file -",; t.reset();
|
2015-09-17 10:01:29 +00:00
|
|
|
img.unparse()
|
2015-09-16 19:14:08 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# image to stdout
|
|
|
|
print "Writing file -",; t.reset();
|
|
|
|
img.write( sys.argv[2] )
|
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
print "\nExecution Time: %s seconds" % total.get()
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
def printIntPalette():
|
|
|
|
img = BMPFile();
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
# lecture du fichier
|
|
|
|
with open( sys.argv[1] ) as file:
|
|
|
|
binFile = file.read()
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
img.parse(binFile);
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
print img.intPalette;
|
2015-09-06 12:54:58 +00:00
|
|
|
|
2015-09-07 08:11:04 +00:00
|
|
|
|
2015-09-06 21:34:26 +00:00
|
|
|
|
|
|
|
|
2015-09-08 20:42:28 +00:00
|
|
|
def testManualCreation():
|
|
|
|
img = BMPFile()
|
|
|
|
for y in range(0, 100):
|
|
|
|
img.content.map.append( [] )
|
2015-09-09 20:12:37 +00:00
|
|
|
for x in range(0, 100):
|
2015-09-09 16:18:21 +00:00
|
|
|
img.content.map[y].append( RGBPixel(
|
2015-09-08 20:42:28 +00:00
|
|
|
random.randint(0, 255),
|
|
|
|
random.randint(0, 255),
|
2015-09-09 20:12:37 +00:00
|
|
|
random.randint(0, 255),
|
|
|
|
bpp=24
|
2015-09-08 20:42:28 +00:00
|
|
|
) );
|
2015-09-06 21:34:26 +00:00
|
|
|
|
2015-09-08 20:42:28 +00:00
|
|
|
img.unparse();
|
2015-09-07 17:21:32 +00:00
|
|
|
|
2015-09-08 20:42:28 +00:00
|
|
|
print img.binData
|
2015-09-07 08:24:28 +00:00
|
|
|
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
print "Require 2 args : \n* input image\n* output image"
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-17 10:01:29 +00:00
|
|
|
def printImageQuality():
|
|
|
|
t = Timer();
|
|
|
|
total = Timer(); total.reset();
|
|
|
|
imageFile, modelFile = "", ""
|
|
|
|
|
|
|
|
|
|
|
|
# lecture des fichiers
|
|
|
|
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())
|
|
|
|
|
|
|
|
# parsage
|
|
|
|
print "Parsing images -",; t.reset();
|
|
|
|
image = BMPFile(); image.parse( imageFile );
|
|
|
|
model = BMPFile(); model.parse( modelFile );
|
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
|
|
|
# condition
|
|
|
|
imagePixelCount = image.header.width * image.header.height
|
|
|
|
modelPixelCount = model.header.width * model.header.height
|
|
|
|
|
|
|
|
if imagePixelCount != modelPixelCount:
|
|
|
|
print "*** Taille de matrices différentes"
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
# comparaison
|
|
|
|
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):
|
|
|
|
count[0] += abs( image.content.map[y][x].r - model.content.map[y][x].r )
|
|
|
|
count[1] += abs( image.content.map[y][x].g - model.content.map[y][x].g )
|
|
|
|
count[2] += abs( image.content.map[y][x].b - model.content.map[y][x].b )
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def imageForImageQuality():
|
|
|
|
t = Timer();
|
|
|
|
total = Timer(); total.reset();
|
|
|
|
imageFile, modelFile = "", ""
|
|
|
|
image, model, newImg = BMPFile(), BMPFile(), BMPFile()
|
2015-09-16 13:37:19 +00:00
|
|
|
|
|
|
|
|
2015-09-17 10:01:29 +00:00
|
|
|
# lecture des fichiers
|
|
|
|
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())
|
|
|
|
|
|
|
|
# parsage
|
|
|
|
print "Parsing images -",; t.reset();
|
|
|
|
image.parse( imageFile );
|
|
|
|
model.parse( modelFile );
|
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
|
|
|
# condition
|
|
|
|
imagePixelCount = image.header.width * image.header.height
|
|
|
|
modelPixelCount = model.header.width * model.header.height
|
|
|
|
|
|
|
|
if imagePixelCount != modelPixelCount:
|
2015-09-17 13:55:16 +00:00
|
|
|
print "*** Taille de images différentes"
|
2015-09-17 10:01:29 +00:00
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
# comparaison
|
|
|
|
print "Comparaison -",; t.reset();
|
|
|
|
count, totalCount = [0,0,0], imagePixelCount*256*3
|
|
|
|
for y in range(0, image.header.height):
|
|
|
|
newImg.content.map.append( [] );
|
|
|
|
for x in range(0, image.header.width):
|
|
|
|
newImg.content.map[y].append( RGBPixel(
|
|
|
|
255 - abs( image.content.map[y][x].r - model.content.map[y][x].r ),
|
|
|
|
255 - abs( image.content.map[y][x].g - model.content.map[y][x].g ),
|
|
|
|
255 - abs( image.content.map[y][x].b - model.content.map[y][x].b )
|
|
|
|
) )
|
|
|
|
|
|
|
|
|
|
|
|
print "Unparsing -",; t.reset();
|
|
|
|
newImg.unparse();
|
|
|
|
print "Done in %s 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 "\nExecution Time: %s seconds" % total.get()
|
|
|
|
|
2015-09-16 13:37:19 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
|
2015-09-17 13:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mergeImages():
|
|
|
|
t = Timer();
|
|
|
|
total = Timer(); total.reset();
|
|
|
|
imageFile, modelFile = "", ""
|
|
|
|
image, model, newImg = BMPFile(), BMPFile(), BMPFile()
|
|
|
|
|
|
|
|
|
|
|
|
# lecture des fichiers
|
|
|
|
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())
|
|
|
|
|
|
|
|
# parsage
|
|
|
|
print "Parsing images -",; t.reset();
|
|
|
|
image.parse( imageFile );
|
|
|
|
model.parse( modelFile );
|
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
|
|
|
|
|
|
|
# condition
|
|
|
|
imagePixelCount = image.header.width * image.header.height
|
|
|
|
modelPixelCount = model.header.width * model.header.height
|
|
|
|
|
|
|
|
if imagePixelCount != modelPixelCount:
|
|
|
|
print "*** Taille de images différentes"
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
# comparaison
|
|
|
|
print "Merging -",; t.reset();
|
|
|
|
for y in range(0, image.header.height):
|
|
|
|
newImg.content.map.append( [] );
|
|
|
|
for x in range(0, image.header.width):
|
|
|
|
newImg.content.map[y].append( RGBPixel(
|
|
|
|
( image.content.map[y][x].r + model.content.map[y][x].r ) % 256,
|
|
|
|
( image.content.map[y][x].g + model.content.map[y][x].g ) % 256,
|
|
|
|
( image.content.map[y][x].b + model.content.map[y][x].b ) % 256
|
|
|
|
) )
|
|
|
|
|
|
|
|
print "Done in %s s" % (t.get())
|
|
|
|
|
|
|
|
print "Unparsing -",; t.reset();
|
|
|
|
newImg.unparse(newBpp=24);
|
|
|
|
print "Done in %s 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 "\nExecution Time: %s seconds" % total.get()
|
|
|
|
|
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
############ TESTS ############
|
2015-09-17 13:55:16 +00:00
|
|
|
# testManualCreation()
|
2015-09-17 10:01:29 +00:00
|
|
|
testSaltAndPepper()
|
2015-09-17 13:55:16 +00:00
|
|
|
# testFileIntegrity()
|
|
|
|
# printIntPalette()
|
|
|
|
printImageQuality()
|
2015-09-16 19:14:08 +00:00
|
|
|
|
2015-09-17 13:55:16 +00:00
|
|
|
# imageForImageQuality()
|
|
|
|
# mergeImages()
|
2015-09-16 19:14:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-17 10:01:29 +00:00
|
|
|
# dure environ 4min 13s
|
2015-09-16 19:14:08 +00:00
|
|
|
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
|
2015-09-17 10:01:29 +00:00
|
|
|
|
2015-09-16 19:14:08 +00:00
|
|
|
|
2015-09-16 19:35:01 +00:00
|
|
|
for seuil in range(0,100,10):
|
2015-09-17 10:01:29 +00:00
|
|
|
for borne in range(0,30,10):
|
2015-09-16 19:14:08 +00:00
|
|
|
|
2015-09-17 10:01:29 +00:00
|
|
|
img.parse( binFile );
|
2015-09-16 19:14:08 +00:00
|
|
|
|
2015-09-16 19:35:01 +00:00
|
|
|
print "SaltAndPepper (%s) (%s) -" % (seuil, borne),; t.reset();
|
2015-09-17 10:01:29 +00:00
|
|
|
noise.SaltAndPepper_unset(img.content.map, seuil=seuil, borne=borne)
|
|
|
|
img.unparse(newBpp=8)
|
|
|
|
img.write( "SaltAndPepper/%s_%s.bmp" % (seuil, borne) )
|
2015-09-16 19:35:01 +00:00
|
|
|
print "Done in %s s" % (t.get())
|
2015-09-16 19:14:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
print "\nExecution Time: %s seconds" % total.get()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
############ CALIBRATE ############
|
2015-09-17 10:01:29 +00:00
|
|
|
#calSaltAndPepper()
|