From ff7a860c2d3075588e4e2f530b26aa045d441f04 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lafreniere Date: Sat, 16 Feb 2019 20:16:47 -0500 Subject: [PATCH 1/3] Get pixel dimensions Add args to input width and height of the image --- pcbdevice/main.py | 22 ++++++++++++++++++++-- pcbdevice/resources/input/square.pbm | 23 +++++++++++++++++++++++ pcbdevice/tests/utils/test_fileUtils.py | 4 ++-- pcbdevice/utils/FileUtils.py | 21 +++++++++++++++++---- 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 pcbdevice/resources/input/square.pbm diff --git a/pcbdevice/main.py b/pcbdevice/main.py index 2cc7f6b..3c62d90 100644 --- a/pcbdevice/main.py +++ b/pcbdevice/main.py @@ -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)) \ No newline at end of file + #plotPath(path(FileUtils.pbmToMatrix(resourcesRawPath + 'test100x100.pbm'), 5)) \ No newline at end of file diff --git a/pcbdevice/resources/input/square.pbm b/pcbdevice/resources/input/square.pbm new file mode 100644 index 0000000..8b34f1c --- /dev/null +++ b/pcbdevice/resources/input/square.pbm @@ -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 diff --git a/pcbdevice/tests/utils/test_fileUtils.py b/pcbdevice/tests/utils/test_fileUtils.py index de95ad3..134536f 100644 --- a/pcbdevice/tests/utils/test_fileUtils.py +++ b/pcbdevice/tests/utils/test_fileUtils.py @@ -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 diff --git a/pcbdevice/utils/FileUtils.py b/pcbdevice/utils/FileUtils.py index 4072edf..08caf56 100644 --- a/pcbdevice/utils/FileUtils.py +++ b/pcbdevice/utils/FileUtils.py @@ -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() \ No newline at end of file + 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') \ No newline at end of file From 2b1feca451501931cdf1b940bad5e49f2efaf7b7 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lafreniere Date: Sat, 16 Feb 2019 20:37:17 -0500 Subject: [PATCH 2/3] Fix tests --- pcbdevice/tests/utils/test_fileUtils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pcbdevice/tests/utils/test_fileUtils.py b/pcbdevice/tests/utils/test_fileUtils.py index 134536f..81f7d10 100644 --- a/pcbdevice/tests/utils/test_fileUtils.py +++ b/pcbdevice/tests/utils/test_fileUtils.py @@ -6,13 +6,13 @@ from pcbdevice.utils.FileUtils import FileUtils resources = './pcbdevice/tests/resources/' class TestFileUtils(TestCase): - def test_pbmToCsv(self): - actual = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm') + def test_pbmToMatrix(self): + actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm') expected = TestUtils.readIntFile(resources + 'formatted/test1.csv') assert actual == expected def test_saveMatrixToFile(self): - actual = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm') + actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm') FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv') expected = TestUtils.readIntFile(resources + 'output/test1.csv') assert actual == expected From 462bab50d3538dbd906533c50eb17a1183818946 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lafreniere Date: Sat, 16 Feb 2019 20:50:37 -0500 Subject: [PATCH 3/3] Unit test of pixel dimension close #33 --- pcbdevice/tests/utils/test_fileUtils.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pcbdevice/tests/utils/test_fileUtils.py b/pcbdevice/tests/utils/test_fileUtils.py index 81f7d10..2ecffa6 100644 --- a/pcbdevice/tests/utils/test_fileUtils.py +++ b/pcbdevice/tests/utils/test_fileUtils.py @@ -16,3 +16,11 @@ class TestFileUtils(TestCase): FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv') expected = TestUtils.readIntFile(resources + 'output/test1.csv') assert actual == expected + + def test_getPixelSize(self): + assert 10, 10 == FileUtils.getPixelSize(10, 10, 100, 100) + assert 1, 1 == FileUtils.getPixelSize(100, 100, 100, 100) + assert 10, 10 == FileUtils.getPixelSize(10, 10, 10, 10, unit = 'cm') + assert 10, 10 == FileUtils.getPixelSize(10, 10, 1, 1, unit = 'm') + assert 254, 254 == FileUtils.getPixelSize(10, 10, 10, 10, unit = 'in') + assert 10, 5 == FileUtils.getPixelSize(10, 10, 10, 20) \ No newline at end of file