How to output multiple images from Blender's compositor with Python? I know how to render one image via Python and get an output with Blender's compositor.

What if I need it to render 100 images for me with numerical names e.g: img_1, img_2, img_3 etc.?

Give me please an example of a working script via Ubuntu terminal and Python sys.argv?

Can I for example setup compositor nodes, and then simply render scene and get composited render as a result render?

Python code for rendering 1 image:

import bpy
import sys

argv = sys.argv
argv = argv[argv.index("--") + 1:]

current_frame_path = argv[0]
print(argv)
ob = bpy.context.active_object
scene_rd = bpy.context.scene.render

scene_rd.filepath = current_frame_path
bpy.ops.render.render(write_still=True)


Shell script for rendering n images

#!/bin/bash

imgs_cnt=n

output_frame_path='RenderedImages/img'


for (( i=1; i&lt;=imgs_cnt; i++ )); do
  echo "Rendering frame ${i}/${imgs_cnt}..."

  (
    cd /path/to/model/
blender --background Model.blend --python Render.py-- "${output_img_path}/img_${i}.png"
  )

  echo "Image ${i}/${imgs_cnt} has been rendered"
done


Here's an example for what I'm trying to do:

This is an automatically generated scene with plants, and I render it via executing shell script in terminal. It use 3 python scripts for blender. 1st script is for generating the scene, it places objects on the scene with different location and rotation settings and check if objects are intersected (to avoid clipping). 2nd script is for rendering scene. 3rd script isn't yet ready but it will be generating a mask for generated scene via cryptomatte.
As a final result I want blender to output me 2 images into 2 different  directories. 1st for the original images and 2nd for mask annotations.
You can add File Output nodes to the compositor to get multiple files saved from any point of your compositing without the need for any Python:



You can also use Python to add more nodes:

import bpy

bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree


for every_node in tree.nodes:
    tree.nodes.remove(every_node)

RenderLayers_node = tree.nodes.new('CompositorNodeRLayers')   
RenderLayers_node.location = 0,0

comp_node = tree.nodes.new('CompositorNodeComposite')   
comp_node.location = 400,0

links = tree.links
link = links.new(RenderLayers_node.outputs[0], comp_node.inputs[0])

for i in range(0,100):
    output_node = tree.nodes.new(type='CompositorNodeOutputFile')
    output_node.name = "Output node number "+str(i+1)
    output_node.location = (400,(-140-i*110)) 
    link = links.new(RenderLayers_node.outputs[0], output_node.inputs[0])


See the documentation for more info.