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"

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)
| improve this answer | |
  • $\begingroup$ Awsome :) It works :) $\endgroup$ – Mortennobel Mar 19 '15 at 8:14
  • $\begingroup$ +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... $\endgroup$ – Sibbs Gambling Nov 17 '16 at 4:43
  • $\begingroup$ 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.) $\endgroup$ – TeaCrab Aug 20 '17 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.