コード例 #1
0
ファイル: test_image_io.py プロジェクト: victoriacity/taichi
def test_image_io_vector(resx, resy, comp, ext, dt):
    shape = (resx, resy)
    pixel = np.random.rand(*shape, comp).astype(to_numpy_type(dt))
    pixel_t = ti.Vector.field(comp, dt, shape)
    pixel_t.from_numpy(pixel)
    fn = test_utils.make_temp_file(suffix='.' + ext)
    ti.imwrite(pixel_t, fn)
    pixel_r = (ti.imread(fn).astype(to_numpy_type(dt)) + 0.5) / 256.0
    assert np.allclose(pixel_r, pixel, atol=2e-2)
    os.remove(fn)
コード例 #2
0
ファイル: test_image_io.py プロジェクト: victoriacity/taichi
def test_image_io_uint(resx, resy, comp, ext, dt):
    shape = (resx, resy)
    np_type = to_numpy_type(dt)
    # When saving to disk, pixel data will be truncated into 8 bits.
    # Be careful here if you want lossless saving.
    np_max = np.iinfo(np_type).max // 256
    pixel = np.random.randint(256, size=(*shape, comp), dtype=np_type) * np_max
    pixel_t = ti.Vector.field(comp, dt, shape)
    pixel_t.from_numpy(pixel)
    fn = test_utils.make_temp_file(suffix='.' + ext)
    ti.imwrite(pixel_t, fn)
    pixel_r = ti.imread(fn).astype(np_type) * np_max
    assert (pixel_r == pixel).all()
    os.remove(fn)
コード例 #3
0
ファイル: test_image_io.py プロジェクト: victoriacity/taichi
def test_image_io(resx, resy, comp, ext, is_field, dt):
    if comp != 1:
        shape = (resx, resy, comp)
    else:
        shape = (resx, resy)
    if is_field:
        pixel_t = ti.field(dt, shape)
    pixel = np.random.randint(256, size=shape, dtype=to_numpy_type(dt))
    if is_field:
        pixel_t.from_numpy(pixel)
    fn = test_utils.make_temp_file(suffix='.' + ext)
    if is_field:
        ti.imwrite(pixel_t, fn)
    else:
        ti.imwrite(pixel, fn)
    pixel_r = ti.imread(fn)
    if comp == 1:
        # from (resx, resy, 1) to (resx, resy)
        pixel_r = pixel_r.reshape((resx, resy))
    assert (pixel_r == pixel).all()
    os.remove(fn)
コード例 #4
0
def test_save_image_without_window(dtype):
    n = 255
    pixels = ti.field(dtype=dtype, shape=(n, n, 3))

    @ti.kernel
    def paint(c: dtype):
        for i, j, k in pixels:
            pixels[i, j, k] = c

    gui = ti.GUI("Test", res=(n, n), show_gui=False)
    for i in [0, 32, 64, 128, 255]:
        if dtype is ti.u8:
            paint(i)
        else:
            paint(i * 1.0 / n)
        gui.set_image(pixels)
        image_path = test_utils.make_temp_file(suffix='.png')
        gui.show(image_path)
        image = ti.imread(image_path)
        delta = (image - i).sum()
        assert delta == 0, "Expected image difference to be 0 but got {} instead.".format(
            delta)