#19 - Reformat pbm file

Convert pbm file to csv with right height and width
This commit is contained in:
Marc-Antoine Lafreniere
2019-01-23 13:57:08 -05:00
parent 6f3c53bda6
commit fffc76ce2e
7 changed files with 5786 additions and 0 deletions

0
utils/__init__.py Normal file
View File

22
utils/pbmformator.py Normal file
View File

@@ -0,0 +1,22 @@
import math
"""
Reformat the file receive by the binary to ascii converter to have matrix with the right width and height
"""
def formatPbm(pbmFile):
dimensionLineIndex = 2
completeFile = []
lines = open(pbmFile, 'r').readlines()
width, height = (int(val) for val in lines[dimensionLineIndex].split())
for line in lines[dimensionLineIndex+1:]:
for val in line.split():
completeFile += [int(val)]
formattedFile = [[0 for i in range(width)] for j in range(height)]
for index, value in enumerate(completeFile):
formattedFile[math.floor(index/width)][index%width] = value
return formattedFile

11
utils/savetofile.py Normal file
View File

@@ -0,0 +1,11 @@
"""
Save a two dimension array in a file
"""
def matrixToFile(matrix, fileName):
with open(fileName, 'w') as f:
for x in matrix:
for y in x:
f.write('%s ' % y )
f.write('\n')