def test_mark_pixels_select_from_img(self): ColorBlockBackgroundRgb = (128, 128, 128) ColorBlockBackgroundRgbDelta = (30, 30, 30) ColorBlockBackgroundGray = 225 ColorBlockBackgroundGrayDelta = 128 # between 225+Delta - 225-Delta pixels are bg pixels FilePathElems = ["test", "test_color_scale_rgb.png"] Img, _ImgId = util.img_load_into_prg_structure__get_imgid(Prg, FilePathElems) InkPixelCoords_and_MarkId = mark_collect.mark_pixels_select_from_img( Prg, Img, ColorBlockBackgroundRgb, ColorBlockBackgroundRgbDelta, ColorBlockBackgroundGray, ColorBlockBackgroundGrayDelta) WantedPixels = {(0, 0): None, (1, 0): None, (2, 0): None, (3, 0): None, (7, 0): None, (8, 0): None, (9, 0): None, (10, 0): None} self.assertEqual(InkPixelCoords_and_MarkId, WantedPixels) # test with grayscale img FilePathElems = ["test", "test_color_scale_gray.png"] Img, _ImgId = util.img_load_into_prg_structure__get_imgid(Prg, FilePathElems) # util.file_write(Prg, "log.txt", str(Img)) InkPixelCoords_and_MarkId = mark_collect.mark_pixels_select_from_img( Prg, Img, ColorBlockBackgroundRgb, ColorBlockBackgroundRgbDelta, ColorBlockBackgroundGray, ColorBlockBackgroundGrayDelta) # util.file_write(Prg, "log.txt", str(InkPixelCoords_and_MarkId)) WantedPixels = {(7, 0): None, (8, 0): None, (9, 0): None, (10, 0): None} self.assertEqual(InkPixelCoords_and_MarkId, WantedPixels)
def test_is_mark_grayscale(self): FilePathElems = ["test", "test_img_grayscale_color_levels.png"] Img, ImgId = util.img_load_into_prg_structure__get_imgid( self.Prg, FilePathElems) ColorBackgroundGrayMin = 10 ColorBackgroundGrayMax = 240 self.assertEqual(util.img_is_grayscale(Img), True) self.assertEqual( mark_collect.is_mark_grayscale(Img, 0, 0, ColorBackgroundGrayMin, ColorBackgroundGrayMax), True) self.assertEqual( mark_collect.is_mark_grayscale(Img, 1, 0, ColorBackgroundGrayMin, ColorBackgroundGrayMax), False) self.assertEqual( mark_collect.is_mark_grayscale(Img, 2, 0, ColorBackgroundGrayMin, ColorBackgroundGrayMax), True) ColorBackgroundGrayMin = 1 ColorBackgroundGrayMax = 255 self.assertEqual( mark_collect.is_mark_grayscale(Img, 0, 0, ColorBackgroundGrayMin, ColorBackgroundGrayMax), False) self.assertEqual( mark_collect.is_mark_grayscale(Img, 1, 0, ColorBackgroundGrayMin, ColorBackgroundGrayMax), False) self.assertEqual( mark_collect.is_mark_grayscale(Img, 2, 0, ColorBackgroundGrayMin, ColorBackgroundGrayMax), False)
def test_mark_ids_collect_from_neighbourhood__mark_ids_set_for_pixel(self): # the second test uses the result of the first one. ######## test: mark_ids_collect_from_neighbourhood ########### Marks = {22: {"Coords":{(2, 2): 0}, "Xmin": 2, "Xmax": 2, "Ymin": 2, "Ymax": 2, "Width": 1, "Height": 1}, 42: {"Coords":{(4, 2): 0, (5, 2): 0}, "Xmin": 4, "Xmax": 5, "Ymin": 2, "Ymax": 2, "Width": 2, "Height": 1}, 44: {"Coords":{(4, 4): 0}, "Xmin": 4, "Xmax": 4, "Ymin": 4, "Ymax": 4, "Width": 1, "Height": 1}} MarkIdIfNoNeighbour = 0 InkPixelCoords_and_MarkId = {(2, 2): 22, (4, 2): 42, (4, 4): 44, (5, 2): 42} Coord = (3, 3) MarkIdsInNeighbourhood, _MarkIdIfNoNeighbour = \ mark_collect.mark_ids_collect_from_neighbourhood(Coord, MarkIdIfNoNeighbour, InkPixelCoords_and_MarkId) WantedMarkIdsInNeighbourhood = [22, 42, 44] self.assertEqual(MarkIdsInNeighbourhood, WantedMarkIdsInNeighbourhood) ######### test: mark_ids_set_for_pixel() ########## MarkIdCurrentPixel = MarkIdsInNeighbourhood.pop(0) # select first id from the possible list FilePathElems = ["test", "test_mark_ids_set_for_pixels_gray.png"] Img, _ImgId = util.img_load_into_prg_structure__get_imgid(Prg, FilePathElems) mark_collect.mark_ids_set_for_pixels(Marks, MarkIdCurrentPixel, InkPixelCoords_and_MarkId, MarkIdsInNeighbourhood, Img, Coord) # util.file_write(Prg, "log_mark.txt", str(Marks)) WantedMarksPixelInserted_and_IdsMerged = {22: {"Coords":{(2, 2): 0, (3, 3): 128, (4, 2): 0, (4, 4): 0, (5, 2): 0}, "Xmin": 2, "Xmax": 5, "Ymin": 2, "Ymax": 4, "Width": 4, "Height": 3, "BoundingBox": 12 }} WantedInkPixelCoords_and_MarkId = {(2, 2): 22, (3, 3): 22, (4, 2): 22, (4, 4): 22, (5, 2): 22} self.assertEqual(Marks, WantedMarksPixelInserted_and_IdsMerged) self.assertEqual(InkPixelCoords_and_MarkId, WantedInkPixelCoords_and_MarkId)
def test_is_mark_rgb(self): FilePathElems = ["test", "test_img_rgb_color_levels.png"] Img, ImgId = util.img_load_into_prg_structure__get_imgid(Prg, FilePathElems) self.assertEqual(util.is_rgb(Img), True) # Bg == Background ColorBgRMin = 0 ColorBgGMin = 0 ColorBgBMin = 0 ColorBgGMax = 40 ColorBgRMax = 40 ColorBgBMax = 40 IsMarkRgb_0_0 = mark_collect.is_mark_rgb(Img, 0, 0, ColorBgRMin, ColorBgGMin, ColorBgBMin, ColorBgGMax, ColorBgRMax, ColorBgBMax, PrintRgb=False) IsMarkRgb_1_0 = mark_collect.is_mark_rgb(Img, 1, 0, ColorBgRMin, ColorBgGMin, ColorBgBMin, ColorBgGMax, ColorBgRMax, ColorBgBMax, PrintRgb=False) IsMarkRgb_2_0 = mark_collect.is_mark_rgb(Img, 2, 0, ColorBgRMin, ColorBgGMin, ColorBgBMin, ColorBgGMax, ColorBgRMax, ColorBgBMax, PrintRgb=False) self.assertEqual(IsMarkRgb_0_0, False) self.assertEqual(IsMarkRgb_1_0, True) self.assertEqual(IsMarkRgb_2_0, True) # The light colors have higher values in RGB. so it can be # the definition of white paper ColorBgRMin = 110 ColorBgGMin = 110 ColorBgBMin = 110 ColorBgGMax = 255 ColorBgRMax = 255 ColorBgBMax = 255 IsMarkRgb_0_0 = mark_collect.is_mark_rgb(Img, 0, 0, ColorBgRMin, ColorBgGMin, ColorBgBMin, ColorBgGMax, ColorBgRMax, ColorBgBMax, PrintRgb=False, PrintRetVal=False) IsMarkRgb_1_0 = mark_collect.is_mark_rgb(Img, 1, 0, ColorBgRMin, ColorBgGMin, ColorBgBMin, ColorBgGMax, ColorBgRMax, ColorBgBMax, PrintRgb=False, PrintRetVal=False) IsMarkRgb_2_0 = mark_collect.is_mark_rgb(Img, 2, 0, ColorBgRMin, ColorBgGMin, ColorBgBMin, ColorBgGMax, ColorBgRMax, ColorBgBMax, PrintRgb=False, PrintRetVal=False) self.assertEqual(IsMarkRgb_0_0, True) self.assertEqual(IsMarkRgb_1_0, False) self.assertEqual(IsMarkRgb_2_0, False)
def mark_collect_from_img_file(Prg, FilePathElems): FilePathElems = [Prg["DirPrgParent"], *FilePathElems] Img, _ImgId = util.img_load_into_prg_structure__get_imgid( Prg, FilePathElems) Marks = mark_collect_from_img_object(Prg, Img) return Marks