-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapCompressor.py
More file actions
38 lines (29 loc) · 1.11 KB
/
Copy pathBitmapCompressor.py
File metadata and controls
38 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from PIL import Image
#Compresses the bits in the Bitmap using RLE Method thing
def compressor(bitmap):
compressedBitmap = []
currentPixel = bitmap[0]
counter = 1
for pixel in bitmap[1:]:
if pixel == currentPixel:
counter += 1
else:
compressedBitmap.append((counter))
currentPixel = pixel
counter = 1
compressedBitmap.append((counter))
return compressedBitmap
#Converts the Compressed integers to binary
def binaryconverter(zahl):
return bin(zahl)[2:]
#Reads the Bitmap file and prints out the compressed Bitmap in decimal form,
#binary form and the lenght of the compressed thing
def main():
with Image.open("TestBitmap.bmp") as img:
bitmap = list(img.getdata())
compressedBitmap = compressor(bitmap)
print("Komprimiertes Bitmap:", compressedBitmap)
binary_compressedBitmap = [binaryconverter(zahl) for zahl in compressedBitmap]
print("Binärzahl davon:", binary_compressedBitmap)
print("Anzahl der komprimierten Binärzahlen:", len(binary_compressedBitmap))
main()