# Write an Image to File with a Specific File Channel Map¶

## Problem¶

You want to write an image to file with a specific file channel map.

## Solution¶

import Draft

# Read an image from file

# Create and set the file channel map
fileChannelMap = { 'R':'16f', 'G':'16f', 'B':'16f', 'A':'16f' }
image.SetFileChannelMap( fileChannelMap )

# Write the image back to file
image.WriteToFile( '//path/to/out.exr' )


## Discussion¶

In order to specify the file channel map of an image you need to create a dictionary that represents a valid file channel map. A valid file channel map will have one entry for each image’s channel and for those channels only. If you are unsure about the channels currently present in the image, you can get the current channel names by using:

print image.GetChannelNames()


To specify the file channel data types, you can consult the table in the section Image File Channel Map for valid file channel data types supported by Draft. Once you have loaded your image file and created a valid file channel map, you can set the file channel map using the method SetFileChannelMap():

image.SetFileChannelMap( fileChannelMap )


When the file is written to file, the file channel map is adjusted according to the valid channel data types for the requested file format.

When you want to add another channel to your image in memory, you can use:

image.SetChannel( 'ID', 4 )


By default, Draft will assign a file channel data type of ‘8’ to the new channel ‘ID’. If you want to set it to another channel data type instead, you can modify the current file channel map using the method GetFileChannelMap() and SetFileChannelMap() in the following way:

fileChannelMap = image.GetFileChannelMap()
fileChannelMap[ 'ID' ] = '32ui'
image.SetFileChannelMap( fileChannelMap )


For more information on the import statement, ReadFromFile() and WriteToFile(), see the Creating an Image section of this Cookbook