def test_command_clean_section_with_palette(self): image_path = fixtures_path('icon.png') image = cv2.imread(image_path) palette = utils.get_palette(image[:, :, ::-1], n_colors=2) clean_image_b64 = self.runner.invoke( cli.clean, [image_path, '-c', 2, '-f', 100, '-p', json.dumps(palette.tolist())] ).output.strip() clean_image = decode_base64(clean_image_b64) section_path = fixtures_path('icon_section.png') section = cv2.imread(section_path) clean_section_b64 = self.runner.invoke( cli.clean, [section_path, '-c', 2, '-f', 100, '-p', json.dumps(palette.tolist())] ).output.strip() clean_section = decode_base64(clean_image_b64) clean_image_colors = sorted(utils.get_color_histogram( clean_image[:, :, ::-1]).keys()) section_colors = sorted(utils.get_color_histogram( section[:, :, ::-1]).keys()) clean_section_colors = sorted(utils.get_color_histogram( clean_section[:, :, ::-1]).keys()) assert 'Error' not in clean_image_b64 assert 'Error' not in clean_section_b64 assert section_colors != clean_section_colors assert clean_section_colors == clean_image_colors
def test_auto_clean_invalid_higher(self): image = self.image test_image = cv2.imread(fixtures_path('clean100.png')) reduce_image = histonets.auto_clean(image, background_value=110, background_saturation=110, colors=150, sample_fraction=110) assert (len(utils.get_color_histogram(test_image)) == len( utils.get_color_histogram(reduce_image)))
def test_auto_clean_non_default(self): image = self.image test_image = cv2.imread(fixtures_path('clean.png')) reduce_image = histonets.auto_clean(image, background_value=30, background_saturation=25, colors=12, sample_fraction=7, white_background=True, saturate=False) assert (len(utils.get_color_histogram(test_image)) != len( utils.get_color_histogram(reduce_image)))
def test_posterization_linear_4_colors_with_palette(self): image = self.image test_image = cv2.imread(fixtures_path('poster_linear4.png')) palette = utils.get_palette(image.reshape((-1, 3)), 4, method='linear') # palette is given as RGB and get_color_histogram returns colors # in the color scheme of image, which by default is BGR reduce_image = histonets.color_reduction(image, 4, 'linear', palette=palette) assert (len(utils.get_color_histogram(test_image)) == len( utils.get_color_histogram(reduce_image)))
def test_posterization_kmeans_4_colors_with_palette(self): image = self.image test_image = cv2.imread(fixtures_path('poster_kmeans4.png')) palette = np.array([ [60, 37, 38], [174, 99, 78], [123, 66, 58], [215, 159, 138], ], dtype=np.uint8) # palette is given as RGB and get_color_histogram returns colors # in the color scheme of image, which by default is BGR reduce_image = histonets.color_reduction(image, 4, 'kmeans', palette=palette) assert (len(utils.get_color_histogram(test_image)) == len( utils.get_color_histogram(reduce_image)))
def test_auto_clean_with_palette(self): image = cv2.imread(fixtures_path('map.png')) palette = utils.get_palette(image[:, :, ::-1], n_colors=8) distances = [] for _ in range(10): clean1 = histonets.auto_clean(image, colors=8, sample_fraction=100, palette=palette) clean2 = histonets.auto_clean(image, colors=8, sample_fraction=100) hist1 = utils.get_color_histogram(clean1) hist1_colors = np.asarray(sorted(hist1.keys())) hist2 = utils.get_color_histogram(clean2) hist2_colors = np.asarray(sorted(hist2.keys())) distances.append(int(np.linalg.norm(hist1_colors - hist2_colors))) distance = np.array(distances).mean() # threshold value is experimentally set as the highest distance # after running the comparison 10k times threshold = 298 # 976 in L*a*b colorspace assert distance <= threshold
def test_auto_clean_section_with_palette(self): image = cv2.imread(fixtures_path('icon.png')) palette = utils.get_palette(image[:, :, ::-1], n_colors=2) clean_image = histonets.auto_clean(image, colors=2, sample_fraction=100, palette=palette) section = cv2.imread(fixtures_path('icon_section.png')) clean_section = histonets.auto_clean(section, colors=2, sample_fraction=100, palette=palette) clean_image_colors = sorted( utils.get_color_histogram(clean_image[:, :, ::-1]).keys()) section_colors = sorted( utils.get_color_histogram(section[:, :, ::-1]).keys()) clean_section_colors = sorted( utils.get_color_histogram(clean_section[:, :, ::-1]).keys()) assert section_colors != clean_section_colors assert clean_section_colors == clean_image_colors
def test_auto_clean(self): image = self.image test_image = cv2.imread(fixtures_path('clean.png')) reduce_image = histonets.auto_clean(image) assert (len(utils.get_color_histogram(test_image)) >= len( utils.get_color_histogram(reduce_image)))
def test_posterization_default_method(self): image = self.image test_image = histonets.color_reduction(image, 4, 'invalid_method') reduce_image = histonets.color_reduction(image, 4, 'kmeans') assert (len(utils.get_color_histogram(test_image)) == len( utils.get_color_histogram(reduce_image)))
def test_posterization_kmeans_invalid_colors_higher(self): image = self.image test_image = cv2.imread(fixtures_path('poster_kmeans128.png')) reduce_image = histonets.color_reduction(image, 500, 'kmeans') assert (len(utils.get_color_histogram(test_image)) == len( utils.get_color_histogram(reduce_image)))