Get pixel dimensions
Add args to input width and height of the image
This commit is contained in:
@@ -1,8 +1,26 @@
|
||||
from pcbdevice.utils.path import path
|
||||
from pcbdevice.utils.plotimg import plotPath
|
||||
from pcbdevice.utils.FileUtils import FileUtils
|
||||
import argparse
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(prog = 'main.py')
|
||||
parser.add_argument('-i', required = True, help = 'PCB image path')
|
||||
parser.add_argument('-wi', required = True, type = int, help = 'Width of the PCB')
|
||||
parser.add_argument('-he', required = True, type = int, help = 'Height of the PCB')
|
||||
parser.add_argument('-u', required = False, help = 'PCB dimension unit')
|
||||
args = parser.parse_args()
|
||||
|
||||
matrix, height, width = FileUtils.pbmToMatrix(args.i)
|
||||
|
||||
if args.u:
|
||||
pxHeight, pxWidth = FileUtils.getPixelSize(height, width, args.he, args.wi, unit = args.u)
|
||||
else:
|
||||
pxHeight, pxWidth = FileUtils.getPixelSize(height, width, args.he, args.wi)
|
||||
|
||||
|
||||
print(pxHeight, pxWidth)
|
||||
|
||||
# Usage example
|
||||
|
||||
resourcesRawPath = 'tests/resources/raw/'
|
||||
@@ -10,6 +28,6 @@ if __name__ == "__main__":
|
||||
resourcesPathOutput = 'resources/pathoutput/'
|
||||
resourcesExpectedPath = 'tests/resources/expected/'
|
||||
|
||||
FileUtils.saveMatrixToFile(FileUtils.pbmToCsv(resourcesRawPath + 'test1ascii.pbm'), resourcesFormattedPath + 'test1.csv')
|
||||
#FileUtils.saveMatrixToFile(FileUtils.pbmToMatrix(resourcesRawPath + 'test1ascii.pbm'), resourcesFormattedPath + 'test1.csv')
|
||||
|
||||
plotPath(path(FileUtils.pbmToCsv(resourcesRawPath + 'test100x100.pbm'), 5))
|
||||
#plotPath(path(FileUtils.pbmToMatrix(resourcesRawPath + 'test100x100.pbm'), 5))
|
||||
23
pcbdevice/resources/input/square.pbm
Normal file
23
pcbdevice/resources/input/square.pbm
Normal file
@@ -0,0 +1,23 @@
|
||||
P1
|
||||
# Bob
|
||||
20 20
|
||||
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0
|
||||
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
|
||||
0 0 2 0 1 1 1 1 1 1 1 1 1 1 1 1 0 2 0 0
|
||||
0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 2 2 2 2 2 2 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 0 0 0 0 0 0 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 2 2 2 2 2 2 2 2 0 1 0 2 0 0
|
||||
0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0
|
||||
0 0 2 0 1 1 1 1 1 1 1 1 1 1 1 1 0 2 0 0
|
||||
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
|
||||
0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0
|
||||
@@ -7,12 +7,12 @@ resources = './pcbdevice/tests/resources/'
|
||||
|
||||
class TestFileUtils(TestCase):
|
||||
def test_pbmToCsv(self):
|
||||
actual = FileUtils.pbmToCsv(resources + 'raw/test1.pbm')
|
||||
actual = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm')
|
||||
expected = TestUtils.readIntFile(resources + 'formatted/test1.csv')
|
||||
assert actual == expected
|
||||
|
||||
def test_saveMatrixToFile(self):
|
||||
actual = FileUtils.pbmToCsv(resources + 'raw/test1.pbm')
|
||||
actual = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm')
|
||||
FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv')
|
||||
expected = TestUtils.readIntFile(resources + 'output/test1.csv')
|
||||
assert actual == expected
|
||||
|
||||
@@ -2,10 +2,10 @@ import math
|
||||
|
||||
class FileUtils:
|
||||
@staticmethod
|
||||
def pbmToCsv(pbmFile, dimensionLineIndex = 2):
|
||||
def pbmToMatrix(pbmFilePath, dimensionLineIndex = 2):
|
||||
completeFile = []
|
||||
|
||||
file = open(pbmFile, 'r')
|
||||
file = open(pbmFilePath, 'r')
|
||||
lines = file.readlines()
|
||||
width, height = (int(val) for val in lines[dimensionLineIndex].split())
|
||||
file.close()
|
||||
@@ -18,7 +18,7 @@ class FileUtils:
|
||||
for index, value in enumerate(completeFile):
|
||||
matrix[int(math.floor(index / width))][index % width] = value
|
||||
|
||||
return matrix
|
||||
return matrix, height, width
|
||||
|
||||
@staticmethod
|
||||
def saveMatrixToFile(matrix, filePath):
|
||||
@@ -27,4 +27,17 @@ class FileUtils:
|
||||
for y in x:
|
||||
f.write('%s ' % y )
|
||||
f.write('\n')
|
||||
f.close()
|
||||
f.close()
|
||||
|
||||
@staticmethod
|
||||
def getPixelSize(matHeight, matWidth, pcbHeight, pcbWidth, unit = 'mm'):
|
||||
if unit == 'mm':
|
||||
return pcbHeight / matHeight, pcbWidth / matWidth
|
||||
elif unit == 'cm':
|
||||
return pcbHeight / matHeight * 10, pcbWidth / matWidth * 10
|
||||
elif unit == 'm':
|
||||
return pcbHeight / matHeight * 100, pcbWidth / matWidth * 100
|
||||
elif unit == 'in':
|
||||
return pcbHeight / matHeight * 25.4, pcbWidth / matWidth * 25.4
|
||||
else:
|
||||
raise RuntimeError('Unit not handle')
|
||||
Reference in New Issue
Block a user