création de la classe BMPHeader

This commit is contained in:
xdrm-brackets 2015-09-06 16:01:17 +02:00
parent 972c1f5c65
commit 0a5c105944
3 changed files with 35 additions and 4 deletions

View File

@ -3,22 +3,26 @@
# TRAITEMENT D'IMAGES # # TRAITEMENT D'IMAGES #
########################### ###########################
# classes
from headerClass import *
import dep import dep
import sys import sys
tmp = "" tmp = ""
header = ""
content = "" content = ""
with open(sys.argv[1]) as f: with open(sys.argv[1]) as f:
for byte in f.read(): for byte in f.read():
content += byte
tmp += str( hex( ord(byte) ) )[2:] + "," tmp += str( hex( ord(byte) ) )[2:] + ","
headerSize = ( 2 + 4 + 2 + 2 + 4 + 4 + 4 + 4 + 2 + 2 + 4 + 4 + 4 + 4 + 4 + 4 ) headerSize = 54
header = tmp[:headerSize]
header = BMPHeader(content[0:headerSize])
content = tmp[headerSize:] content = tmp[headerSize:]
print header print header.offset
print print
print content print content

27
code/headerClass.py Normal file
View File

@ -0,0 +1,27 @@
# ~*~ encoding: utf-8 ~*~ #
class BMPHeader:
def toInt(self, bytes):
intReturn = 0;
for i, byte in enumerate(bytes):
intReturn += ord(byte) * (256 ** i)
return intReturn
# CONSTRUCTEUR: parse le header au format bin en objet
def __init__(self, binHeader):
self.signature = self.toInt(binHeader[ 0: 2]) # signature (4D42)
self.fileSize = self.toInt(binHeader[ 2: 6]) # taille du fichier bmp (bytes)
self.offset = self.toInt(binHeader[10:14]) # début de l'image (bytes)
self.infoSize = self.toInt(binHeader[14:18]) # taille du INFO_HEADER
self.width = self.toInt(binHeader[18:22]) # longueur de l'image (pixel)
self.height = self.toInt(binHeader[22:26]) # hauteur de l'image (pixel)
self.plans = self.toInt(binHeader[26:28]) # nombre de plans (default: 1)
self.bpp = self.toInt(binHeader[28:30]) # nombre de bits par pixel (1,4,8, 24)
self.compType = self.toInt(binHeader[30:34]) # type de compression (0=none, 1=RLE-8, 2=RLE-4)
self.size = self.toInt(binHeader[34:38]) # taille de l'image avec padding (bytes)
self.horiRes = self.toInt(binHeader[38:42]) # résolution horizontale (pixels)
self.vertRes = self.toInt(binHeader[42:46]) # résolution verticale (pixels)
self.colorNb = self.toInt(binHeader[46:50]) # nombre de couleurs de l'image (ou 0)
self.colorINb = self.toInt(binHeader[50:54]) # nombre d'images importantes (ou 0)
self.header = binHeader

BIN
code/headerClass.pyc Normal file

Binary file not shown.