def test_to_type_extreme_value(self): types = ['uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64'] max_of_type = dict(bool=True, uint8=255, uint16=65535, uint32=4294967295, uint64=18446744073709551615, float32=1.0, float64=1.0) for src_dtype in types + ['bool']: for dst_dtype in types: for shape in [(), (1, ), (2, 2)]: src_value = max_of_type[src_dtype] src = np.full(shape, src_value, dtype=src_dtype) dst = media.to_type(src, dst_dtype) dst_value = dst.flat[0] expected_value = max_of_type[dst_dtype] msg = f'{src_dtype} {dst_dtype} {shape} {src} {dst}' self.assertEqual(dst.dtype, dst_dtype, msg=msg) self.assertEqual(dst.shape, src.shape, msg=msg) self.assertEqual(dst_value, expected_value, msg=msg)
def test_video_read_write_10bit(self): shape = (256, 256) num_images = 4 fps = 60 bps = 40_000_000 horizontal_gray_ramp = media.to_type( np.indices(shape)[1] / shape[1], np.uint16) video = np.broadcast_to(horizontal_gray_ramp, (num_images, *shape)) with tempfile.TemporaryDirectory() as directory_name: filename = os.path.join(directory_name, 'test3.mp4') media.write_video(filename, video, fps=fps, bps=bps, encoded_format='yuv420p10le') new_video = media.read_video(filename, dtype=np.uint16, output_format='gray') self.assertEqual(new_video.dtype, np.uint16) value_1_of_10bit_encoded_in_16bits = 64 self._check_similar(video, new_video, max_rms=value_1_of_10bit_encoded_in_16bits * 0.8)
def check(src, dtype, expected): output = media.to_type(src, dtype) self.assertEqual(output.dtype.type, np.dtype(dtype).type) self.assert_all_equal(output, expected)