# Saving a 16 bit PNG using script

I'm trying to bake displacements using script but need to save the result using a 16 bit image format (such as PNG-16). How can I change the color-depth setting using script?

Example code

img = bpy.data.images.new(name="depthmap", width=imageSize,height=imageSize,alpha=False, float_buffer=True)
img.colorspace_settings.name = 'Linear'
# bake() #implemented in separate function
img.file_format = 'PNG'
# need to set to 16-bit here
img.filepath_raw = "my_new_bake.png"
img.save()


You can not directly assign the color depth on the image. However, you can make a scene and set its render.image_settings.color_depth. Then use save_render() with that scene to save the image with the color depth.

import bpy

# Create new scene to set color depth on it
scene = bpy.data.scenes.new('scene name')
settings = scene.render.image_settings
settings.color_depth = '16'
settings.file_format = 'PNG'

# Create new image and save with scene
img = bpy.data.images.new('name', width, height)
img.save_render('filepath', scene)

• Awsome :) It works :) – Mortennobel Mar 19 '15 at 8:14
• +1 -- it works! But it's weird that why here 16 needs to be a string, whereas, for example, scene.render.resolution_x can be just an integer... – Sibbs Gambling Nov 17 '16 at 4:43
• the bit-depth is a preset option. For example, a bit-depth of 7 simply won't work. That's why there is no point to make the option taking in an int value. Else, maybe it's for the safety that someone will try to put in an int 7, and cause the computer to "Blow up" because an actual bit-depth of 7 is impossible.(It can be faked using a bit depth 8 or higher, but will always take up 8 bits amount of data per pixel/channel, unless you've written your own bit-packing algorithm to pack the 7 bit-depth image into a 8 bit data array.) – TeaCrab Aug 20 '17 at 4:17