def test_match_imgs(self): """Test if the automatic conversion between modes works as intended.""" images = imgs.get_imgs() for img in images: with imgs.TempFile(img) as img1_path: with imgs.TempFile(images[0]) as img2_path: img1, img2 = analyse.match_imgs(img1_path, img2_path) self.assertEqual(img1.mode, img2.mode)
def test_save_img(self): img = imgs.create_rgb() # when out_path is None, this should not write any file with imgs.TempFile() as path: analyse.save_img(img, None) self.assertFalse(os.path.isfile(path)) # when out_path is set to a string, save_img should save the file with imgs.TempFile() as path: analyse.save_img(img, path) self.assertTrue(os.path.isfile(path))
def test_save_img_array(self): img_array = scipy.misc.lena() # when out_path is None, save_img_array should not write any file with imgs.TempFile() as path: analyse.save_img_array(img_array, None) self.assertFalse(os.path.isfile(path)) # when out_path is set to a string, save_img_array should save the file with imgs.TempFile() as path: analyse.save_img_array(img_array, path) self.assertTrue(os.path.isfile(path))
def test_find_strings_invalid_threshold(self): img = scipy.misc.lena() with imgs.TempFile(img) as path: with self.assertRaisesRegexp(AssertionError, '[tT]hreshold'): analyse.find_strings(img_path=path, threshold=0) with self.assertRaisesRegexp(AssertionError, '[tT]hreshold'): analyse.find_strings(img_path=path, threshold=-1)
def test_open_img_array(self): img_array = scipy.misc.lena() # given an numpy.ndarray, open_img_array should return the same inst self.assertEqual(id(img_array), id(analyse.open_img_array(img_array))) # given an path, open_img_array should return an numpy.ndarray with imgs.TempFile(img_array) as img_path: opened_array = analyse.open_img_array(img_path) self.assertTrue(isinstance(opened_array, np.ndarray))
def test_open_img(self): img = imgs.create_rgb() # given an Pillow Image, open_img should return the same instance self.assertEqual(id(img), id(analyse.open_img(img))) # given an path, open_img should return a Pillow Image instance with imgs.TempFile(img) as img_path: opened_img = analyse.open_img(img_path) self.assertTrue(isinstance(opened_img, Image.Image)) self.assertEqual(list(img.getdata()), list(opened_img.getdata()))
def test_find_diffs(self): DIFF_POS = (20, 20) for img in imgs.get_imgs(): diff_color = imgs.sub_from_color(img.color, 12) diff_img = img.copy() diff_img.putpixel(DIFF_POS, diff_color) with imgs.TempFile() as path: analyse.find_diffs(img, diff_img, out_path=path) out_img = Image.open(path) self.assertEqual((255, 0, 0), out_img.getpixel(DIFF_POS)) self.assertEqual((0, 0, 0), out_img.getpixel((0, 0)))
def test_fix_palette_output(self): img = self.create_palette_img() with imgs.TempFile() as tmp_path: analyse.fix_palette(img, out_path=tmp_path) self.assertTrue(os.path.isfile(tmp_path))
def test_modify_output(self): img = imgs.get_imgs()[0] with imgs.TempFile() as tmp_path: analyse.modify('value ^ 177', img, out_path=tmp_path) self.assertTrue(os.path.isfile(tmp_path))
def test_combine_output(self): img1_array = scipy.misc.lena() img2_array = scipy.misc.lena() with imgs.TempFile() as tmp_path: analyse.combine('img1 ^ img2', img1_array, img2_array, tmp_path) self.assertTrue(os.path.isfile(tmp_path))