Writing an exr image one scan line at a time in Python

340 Views Asked by At

I've been assigned to write a tool that stitches exr files together (in a grid of any size, be it 2x2 or 4x4). When writing the output image, the memory footprint should be small. enter image description here

How do I write the output image one scan line at a time?

This is my code so far:

for i in range(1, len(exr_files_list)):
   if i == 1:
      merged = exr_file_list[0]
   else:
      merged = merge_images(merged, exr_files_list[i])

merged_image_array = np.array(merged)
write_exr('a.exr', merged_image_array, ['R', 'G', 'B']

def write_exr(filename, values, channel_names):
  """Writes the values in a multi-channel ndarray into an EXR file.

  Args:
    filename: The filename of the output file
    values: A numpy ndarray with shape [height, width, channels]
    channel_names: A list of strings with length = channels

  Raises:
    TypeError: If the numpy array has an unsupported type.
    ValueError: If the length of the array and the length of the channel names
      list do not match.
  """
  if values.shape[-1] != len(channel_names):
    raise ValueError(
        'Number of channels in values does not match channel names (%d, %d)' %
        (values.shape[-1], len(channel_names)))
  header = openexr.Header(values.shape[1], values.shape[0])
  try:
    exr_channel_type = Imath.PixelType(_np_to_exr[values.dtype.type])
  except KeyError:
    raise TypeError('Unsupported numpy type: %s' % str(values.dtype))
  header['channels'] = {
      n: Imath.Channel(exr_channel_type) for n in channel_names
  }
  channel_data = [values[..., i] for i in range(values.shape[-1])]
  exr = openexr.OutputFile(filename, header)
  exr.writePixels(
      dict((n, d.tobytes()) for n, d in zip(channel_names, channel_data)))
  exr.close()
0

There are 0 best solutions below