Save all images in a file using "save as render" via python script

I’ve created a simple script that bakes lightmaps on a lot of selected objects in blender 2.93.4.

The last line of the code simply saves all the images, the problem is that I need them to be saved as render , using the filmic transform applied to the final images (like when you save every image manually and check “save as render” in the file browser save window). How can I do that? Right now it simply saves them in sRGB color transform. Of course filmic is active and selected as view transform in color management options, and I know I can set the display image option changing this:


but it only affect view, and not saving.

I’m also aware of


Which has a save_as_render parameter, but It’s an operator, and it needs context, while in my case I just need to save all my images, even if the Image editor is not even open.

here is the code:

import bpy

bpy.ops.outliner.orphans_purge(do_recursive = True)

objects = bpy.context.selected_objects

for obj in objects:
    if obj.type == 'MESH':
        mat = obj.material_slots[0].material
        obj_name =
        obj_name_len = len(obj_name)

        mat.use_nodes = True
        node_tree = mat.node_tree
        nodes = node_tree.nodes
        tex_node_exists = False
        for node in nodes:
            node_name_prefix =[:obj_name_len]  
            node_name = 
            if node_name_prefix == obj_name:
                tex_node_exists = True
        if tex_node_exists == False:
            tex_image = + "_LM", bake_resolution, bake_resolution, alpha=True)
            tex_image.filepath = "//" + obj_name + "_LM.png"

            tex_node ='ShaderNodeTexImage')
            tex_node.image = tex_image

   = True
   = tex_node
            tex_node_exists = True

bpy.ops.object.bake(save_mode='EXTERNAL',filepath= "//")


Thanks in advance

I Solved it by storing baked images in a list and using


on all of them at the end of the script (and avoided saving them before the bake, otherwise the save_render() method would fail to overwrite them).