def test_negative_index(): n = 10 array = _array_2d_to_RGB(np.arange(0, n)[np.newaxis, :]) # Test both x and y indices. pic = novice.Picture(array=array) assert pic[-1, 0] == pic[n - 1, 0] pic = novice.Picture(array=rgb_transpose(array)) assert pic[0, -1] == pic[0, n - 1]
def test_negative_slice(): n = 10 array = _array_2d_to_RGBA(np.arange(0, n)[np.newaxis, :]) # Test both x and y slices. pic = novice.Picture(array=array) assert pic[-3:, 0] == pic[n - 3:, 0] pic = novice.Picture(array=rgb_transpose(array)) assert pic[0, -3:] == pic[0, n - 3:]
def test_picture_slice(): array = _array_2d_to_RGBA(np.arange(0, 10)[np.newaxis, :]) pic = novice.Picture(array=array) x_slice = slice(3, 8) subpic = pic[:, x_slice] assert_allclose(subpic.array, array[x_slice, :])
def test_indexing(): array = 128 * np.ones((10, 10, 3), dtype=np.uint8) pic = novice.Picture(array=array) pic[0:5, 0:5] = (0, 0, 0) for p in pic: if (p.x < 5) and (p.y < 5): assert_equal(p.rgb, (0, 0, 0)) assert_equal(p.red, 0) assert_equal(p.green, 0) assert_equal(p.blue, 0) pic[:5, :5] = (255, 255, 255) for p in pic: if (p.x < 5) and (p.y < 5): assert_equal(p.rgb, (255, 255, 255)) assert_equal(p.red, 255) assert_equal(p.green, 255) assert_equal(p.blue, 255) pic[5:pic.width, 5:pic.height] = (255, 0, 255) for p in pic: if (p.x >= 5) and (p.y >= 5): assert_equal(p.rgb, (255, 0, 255)) assert_equal(p.red, 255) assert_equal(p.green, 0) assert_equal(p.blue, 255) pic[5:, 5:] = (0, 0, 255) for p in pic: if (p.x >= 5) and (p.y >= 5): assert_equal(p.rgb, (0, 0, 255)) assert_equal(p.red, 0) assert_equal(p.green, 0) assert_equal(p.blue, 255)
def test_modified_on_set_pixel(): data = np.zeros(shape=(10, 5, 3), dtype=np.uint8) pic = novice.Picture(array=data) pixel = pic[0, 0] pixel.green = 1 assert pic.modified
def test_save_with_alpha_channel(): # create an image with an alpha channel pic = novice.Picture(array=np.zeros((3, 3, 4))) fd, filename = tempfile.mkstemp(suffix=".jpg") os.close(fd) pic.save(filename) os.unlink(filename)
def test_move_slice(): h, w = 3, 12 array = _array_2d_to_RGBA(np.linspace(0, 255, h * w).reshape(h, w)) array = array.astype(np.uint8) pic = novice.Picture(array=array) pic_orig = novice.Picture(array=array.copy()) # Move left cut of image to the right side. cut = 5 rest = pic.width - cut temp = pic[:cut, :] temp.array = temp.array.copy() pic[:rest, :] = pic[cut:, :] pic[rest:, :] = temp assert pic[rest:, :] == pic_orig[:cut, :] assert pic[:rest, :] == pic_orig[cut:, :]
def test_save_with_alpha_channel(): # create an image with an alpha channel pic = novice.Picture(array=np.zeros((3, 3, 4))) fd, filename = tempfile.mkstemp(suffix=".png") os.close(fd) with expected_warnings(['is a low contrast']): pic.save(filename) os.unlink(filename)
def test_update_on_save(): pic = novice.Picture(array=np.zeros((3, 3, 3))) pic.size = (6, 6) assert pic.modified assert pic.path is None with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp: pic.save(tmp.name) assert not pic.modified assert_equal(pic.path, os.path.abspath(tmp.name)) assert_equal(pic.format, "jpeg")
def test_update_on_save(): pic = novice.Picture(array=np.zeros((3, 3, 3))) pic.size = (6, 6) assert pic.modified assert pic.path is None fd, filename = tempfile.mkstemp(suffix=".jpg") os.close(fd) try: pic.save(filename) assert not pic.modified assert_equal(pic.path, os.path.abspath(filename)) assert_equal(pic.format, "jpeg") finally: os.unlink(filename)
def test_update_on_save(): pic = novice.Picture(array=np.zeros((3, 3, 3))) # prevent attempting to save low-contrast image pic[0, 0] = (255, 255, 255) with all_warnings(): # precision loss pic.size = (6, 6) assert pic.modified assert pic.path is None fd, filename = tempfile.mkstemp(suffix=".jpg") os.close(fd) try: pic.save(filename) assert not pic.modified assert_equal(pic.path, os.path.abspath(filename)) assert_equal(pic.format, "jpeg") finally: os.unlink(filename)
def test_getitem_with_step(): h, w = 5, 5 array = _array_2d_to_RGBA(np.linspace(0, 255, h * w).reshape(h, w)) pic = novice.Picture(array=array) sliced_pic = pic[::2, ::2] assert sliced_pic == novice.Picture(array=array[::2, ::2])
def test_reset(): pic = novice.Picture(SMALL_IMAGE_PATH) v = pic[0, 0] pic[0, 0] = (1, 1, 1) pic.reset() assert pic[0, 0] == v
def test_modified_on_set(): pic = novice.Picture(SMALL_IMAGE_PATH) pic[0, 0] = (1, 1, 1) assert pic.modified assert pic.path is None