def test_util_image_as_uint(): """ Tests the various type conversions when writing to uint""" raises(ValueError, core.image_as_uint, 4) raises(ValueError, core.image_as_uint, "not an image") raises(ValueError, core.image_as_uint, np.array([0, 1]), bitdepth=13) raises(ValueError, core.image_as_uint, np.array([2.0, 2.0], 'float32')) raises(ValueError, core.image_as_uint, np.array([0.0, np.inf], 'float32')) raises(ValueError, core.image_as_uint, np.array([-np.inf, 0.0], 'float32')) test_arrays = ( # (input, output bitdepth, expected output) # No bitdepth specified, assumed to be 8-bit (np.array([0, 2**8 - 1], 'uint8'), None, np.uint8([0, 255])), (np.array([0, 2**16 - 1], 'uint16'), None, np.uint8([0, 255])), (np.array([0, 2**32 - 1], 'uint32'), None, np.uint8([0, 255])), (np.array([0, 2**64 - 1], 'uint64'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int8'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int16'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int32'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int64'), None, np.uint8([0, 255])), (np.array([0, 1], 'float16'), None, np.uint8([0, 255])), (np.array([0, 1], 'float32'), None, np.uint8([0, 255])), (np.array([0, 1], 'float64'), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float16'), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float32'), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float64'), None, np.uint8([0, 255])), # 8-bit output (np.array([0, 2**8 - 1], 'uint8'), 8, np.uint8([0, 255])), (np.array([0, 2**16 - 1], 'uint16'), 8, np.uint8([0, 255])), (np.array([0, 2**32 - 1], 'uint32'), 8, np.uint8([0, 255])), (np.array([0, 2**64 - 1], 'uint64'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int8'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int16'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int32'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int64'), 8, np.uint8([0, 255])), (np.array([0, 1], 'float16'), 8, np.uint8([0, 255])), (np.array([0, 1], 'float32'), 8, np.uint8([0, 255])), (np.array([0, 1], 'float64'), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float16'), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float32'), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float64'), 8, np.uint8([0, 255])), # 16-bit output (np.array([0, 2**8 - 1], 'uint8'), 16, np.uint16([0, 65535])), (np.array([0, 2**16 - 1], 'uint16'), 16, np.uint16([0, 65535])), (np.array([0, 2**32 - 1], 'uint32'), 16, np.uint16([0, 65535])), (np.array([0, 2**64 - 1], 'uint64'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int8'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int16'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int32'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int64'), 16, np.uint16([0, 65535])), (np.array([0, 1], 'float16'), 16, np.uint16([0, 65535])), (np.array([0, 1], 'float32'), 16, np.uint16([0, 65535])), (np.array([0, 1], 'float64'), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], 'float16'), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], 'float32'), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], 'float64'), 16, np.uint16([0, 65535])), ) for tup in test_arrays: res = core.image_as_uint(tup[0], bitdepth=tup[1]) assert res[0] == tup[2][0] and res[1] == tup[2][1]
def test_util_image_as_uint(): ''' Tests the various type conversions when writing to uint''' raises(ValueError, core.image_as_uint, 4) raises(ValueError, core.image_as_uint, "not an image") raises(ValueError, core.image_as_uint, np.array([0, 1]), bitdepth=13) raises(ValueError, core.image_as_uint, np.array([2.0, 2.0], 'float32')) raises(ValueError, core.image_as_uint, np.array([0.0, np.inf], 'float32')) raises(ValueError, core.image_as_uint, np.array([-np.inf, 0.0], 'float32')) test_arrays = ( # (input, output bitdepth, expected output) # No bitdepth specified, assumed to be 8-bit (np.array([0, 2 ** 8 - 1], 'uint8'), None, np.uint8([0, 255])), (np.array([0, 2 ** 16 - 1], 'uint16'), None, np.uint8([0, 255])), (np.array([0, 2 ** 32 - 1], 'uint32'), None, np.uint8([0, 255])), (np.array([0, 2 ** 64 - 1], 'uint64'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int8'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int16'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int32'), None, np.uint8([0, 255])), (np.array([-2, 2], 'int64'), None, np.uint8([0, 255])), (np.array([0, 1], 'float16'), None, np.uint8([0, 255])), (np.array([0, 1], 'float32'), None, np.uint8([0, 255])), (np.array([0, 1], 'float64'), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float16'), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float32'), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float64'), None, np.uint8([0, 255])), # 8-bit output (np.array([0, 2 ** 8 - 1], 'uint8'), 8, np.uint8([0, 255])), (np.array([0, 2 ** 16 - 1], 'uint16'), 8, np.uint8([0, 255])), (np.array([0, 2 ** 32 - 1], 'uint32'), 8, np.uint8([0, 255])), (np.array([0, 2 ** 64 - 1], 'uint64'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int8'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int16'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int32'), 8, np.uint8([0, 255])), (np.array([-2, 2], 'int64'), 8, np.uint8([0, 255])), (np.array([0, 1], 'float16'), 8, np.uint8([0, 255])), (np.array([0, 1], 'float32'), 8, np.uint8([0, 255])), (np.array([0, 1], 'float64'), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float16'), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float32'), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], 'float64'), 8, np.uint8([0, 255])), # 16-bit output (np.array([0, 2 ** 8 - 1], 'uint8'), 16, np.uint16([0, 65535])), (np.array([0, 2 ** 16 - 1], 'uint16'), 16, np.uint16([0, 65535])), (np.array([0, 2 ** 32 - 1], 'uint32'), 16, np.uint16([0, 65535])), (np.array([0, 2 ** 64 - 1], 'uint64'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int8'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int16'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int32'), 16, np.uint16([0, 65535])), (np.array([-2, 2], 'int64'), 16, np.uint16([0, 65535])), (np.array([0, 1], 'float16'), 16, np.uint16([0, 65535])), (np.array([0, 1], 'float32'), 16, np.uint16([0, 65535])), (np.array([0, 1], 'float64'), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], 'float16'), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], 'float32'), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], 'float64'), 16, np.uint16([0, 65535])),) for tup in test_arrays: res = core.image_as_uint(tup[0], bitdepth=tup[1]) assert res[0] == tup[2][0] and res[1] == tup[2][1]
def _append_data(self, im, meta): # Get props of image size = im.shape[:2] depth = 1 if im.ndim == 2 else im.shape[2] # Ensure that image is in uint8 im = image_as_uint(im, bitdepth=8) # Set size and initialize if not initialized yet if self._size is None: map = {1: 'gray', 2: 'gray8a', 3: 'rgb24', 4: 'rgba'} self._pix_fmt = map.get(depth, None) if self._pix_fmt is None: raise ValueError('Image must have 1, 2, 3 or 4 channels') self._size = size self._depth = depth self._initialize() # Check size of image if size != self._size: raise ValueError('All images in a movie should have same size') if depth != self._depth: raise ValueError('All images in a movie should have same ' 'number of channels') assert self._proc is not None # Check status # Write try: self._proc.stdin.write(im.tostring()) except IOError as e: # Show the command and stderr from pipe msg = '{0:}\n\nFFMPEG COMMAND:\n{1:}\n\nFFMPEG STDERR ' \ 'OUTPUT:\n'.format(e, self._cmd) raise IOError(msg)
def test_util_image_as_uint(): """ Tests the various type conversions when writing to uint""" raises(ValueError, core.image_as_uint, 4) raises(ValueError, core.image_as_uint, "not an image") raises(ValueError, core.image_as_uint, np.array([0, 1]), bitdepth=13) raises(ValueError, core.image_as_uint, np.array([2.0, 2.0], "float32")) raises(ValueError, core.image_as_uint, np.array([0.0, np.inf], "float32")) raises(ValueError, core.image_as_uint, np.array([-np.inf, 0.0], "float32")) test_arrays = ( # (input, output bitdepth, expected output) # No bitdepth specified, assumed to be 8-bit (np.array([0, 2 ** 8 - 1], "uint8"), None, np.uint8([0, 255])), (np.array([0, 2 ** 16 - 1], "uint16"), None, np.uint8([0, 255])), (np.array([0, 2 ** 32 - 1], "uint32"), None, np.uint8([0, 255])), (np.array([0, 2 ** 64 - 1], "uint64"), None, np.uint8([0, 255])), (np.array([-2, 2], "int8"), None, np.uint8([0, 255])), (np.array([-2, 2], "int16"), None, np.uint8([0, 255])), (np.array([-2, 2], "int32"), None, np.uint8([0, 255])), (np.array([-2, 2], "int64"), None, np.uint8([0, 255])), (np.array([0, 1], "float16"), None, np.uint8([0, 255])), (np.array([0, 1], "float32"), None, np.uint8([0, 255])), (np.array([0, 1], "float64"), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], "float16"), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], "float32"), None, np.uint8([0, 255])), (np.array([-1.0, 1.0], "float64"), None, np.uint8([0, 255])), # 8-bit output (np.array([0, 2 ** 8 - 1], "uint8"), 8, np.uint8([0, 255])), (np.array([0, 2 ** 16 - 1], "uint16"), 8, np.uint8([0, 255])), (np.array([0, 2 ** 32 - 1], "uint32"), 8, np.uint8([0, 255])), (np.array([0, 2 ** 64 - 1], "uint64"), 8, np.uint8([0, 255])), (np.array([-2, 2], "int8"), 8, np.uint8([0, 255])), (np.array([-2, 2], "int16"), 8, np.uint8([0, 255])), (np.array([-2, 2], "int32"), 8, np.uint8([0, 255])), (np.array([-2, 2], "int64"), 8, np.uint8([0, 255])), (np.array([0, 1], "float16"), 8, np.uint8([0, 255])), (np.array([0, 1], "float32"), 8, np.uint8([0, 255])), (np.array([0, 1], "float64"), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], "float16"), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], "float32"), 8, np.uint8([0, 255])), (np.array([-1.0, 1.0], "float64"), 8, np.uint8([0, 255])), # 16-bit output (np.array([0, 2 ** 8 - 1], "uint8"), 16, np.uint16([0, 65535])), (np.array([0, 2 ** 16 - 1], "uint16"), 16, np.uint16([0, 65535])), (np.array([0, 2 ** 32 - 1], "uint32"), 16, np.uint16([0, 65535])), (np.array([0, 2 ** 64 - 1], "uint64"), 16, np.uint16([0, 65535])), (np.array([-2, 2], "int8"), 16, np.uint16([0, 65535])), (np.array([-2, 2], "int16"), 16, np.uint16([0, 65535])), (np.array([-2, 2], "int32"), 16, np.uint16([0, 65535])), (np.array([-2, 2], "int64"), 16, np.uint16([0, 65535])), (np.array([0, 1], "float16"), 16, np.uint16([0, 65535])), (np.array([0, 1], "float32"), 16, np.uint16([0, 65535])), (np.array([0, 1], "float64"), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], "float16"), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], "float32"), 16, np.uint16([0, 65535])), (np.array([-1.0, 1.0], "float64"), 16, np.uint16([0, 65535])), # Rounding (np.array([1.4 / 255, 1.6 / 255], "float32"), 8, np.uint8([1, 2])), (np.array([254.4 / 255, 254.6 / 255], "float32"), 8, np.uint8([254, 255])), (np.array([1.4 / 65535, 1.6 / 65535], "float32"), 16, np.uint16([1, 2])), ( np.array([65534.4 / 65535, 65534.6 / 65535], "float32"), 16, np.uint16([65534, 65535]), ), # noqa ) for tup in test_arrays: res = core.image_as_uint(tup[0], bitdepth=tup[1]) assert res[0] == tup[2][0] and res[1] == tup[2][1]