コード例 #1
0
def exr_channel_to_numpy(exr_file: OpenEXR.InputFile,
                         channel_name: str,
                         reshape: Optional[Tuple[int, int]],
                         dtype: ExrDtype = ExrDtype.FLOAT32) -> np.ndarray:
    """Extracts a channel in an EXR file into a numpy array

    Args:
        exr_file (OpenEXR.InputFile): The opened EXR file object
        channel_name (str): The name of the channel to be converted to numpy
        reshape (None or Tuple(height, width): If given, will reshape the 2D numpy array into (height, width)
        dtype (ExrDtype): Whether the data in channel is of float32 or float16 type

    Returns:
        numpy.ndarray: The extracted channel in form of numpy array
    """
    if dtype == ExrDtype.FLOAT32:
        point_type = Imath.PixelType(Imath.PixelType.FLOAT)
        np_type = np.float32
    else:
        point_type = Imath.PixelType(Imath.PixelType.HALF)
        np_type = np.float16

    channel_arr = np.frombuffer(exr_file.channel(channel_name, point_type),
                                dtype=np_type)
    if reshape:
        channel_arr = channel_arr.reshape(reshape)

    return channel_arr
コード例 #2
0
ファイル: exr.py プロジェクト: MoonShineVFX/exr-separator
 def get_pixels_data(self, exr: OpenEXR.InputFile) -> dict:
     pixels_data = {}
     for i, target_channel in enumerate(self.get_target_channels()):
         label_index = i
         if self.is_depth():
             label_index = 0
         pixels_data[target_channel] = exr.channel(self.get_labels()[label_index], self.get_pixel_type())
     return pixels_data
コード例 #3
0
def read_channels_from_exr(exr: OpenEXR.InputFile,
                           channel_names: Sequence[str]) -> np.ndarray:
    """Reads a single channel from an EXR file and returns it as a numpy array."""
    channels_header = exr.header()['channels']
    window = exr.header()['dataWindow']
    width = window.max.x - window.min.x + 1
    height = window.max.y - window.min.y + 1
    outputs = []
    for channel_name in channel_names:
        channel_type = channels_header[channel_name].type.v
        numpy_type = {
            Imath.PixelType.HALF: np.float16,
            Imath.PixelType.FLOAT: np.float32,
            Imath.PixelType.UINT: np.uint32,
        }[channel_type]
        array = np.frombuffer(exr.channel(channel_name), numpy_type)
        array = array.reshape([height, width])
        outputs.append(array)
    # TODO: verify that the types are all the same?
    return np.stack(outputs, axis=-1)
コード例 #4
0
  def __getitem__(self, index):

    if isinstance(index, str):
      return super(ImageSequence, self).__getitem__(index)

    if index == self.cache_index:
      return self.cache * numpy.exp(self.exposure_offset)

    if not isinstance(index, int) and index in self.frame_range:
      raise IndexError

    exrfile = InputFile(self.file_pattern % index)

    channels = ('R', 'G', 'B')

    output = numpy.ndarray(dtype=numpy.float, shape=(self.height, self.width, channels.__len__()))

    for channel_index, channel in enumerate(channels):
      output[:, :, channel_index] = numpy.fromstring(exrfile.channel(channel, Imath.PixelType(Imath.PixelType.HALF)), dtype=numpy.float16).astype(numpy.float).reshape([self.height, self.width])

    self.cache = output
    self.cache_index = index

    return output * numpy.exp(self.exposure_offset)