How to load and assign an image to a specific image node in the compositor? Background: I am importing (appending) a new scene to an existing .blend file. The new scene has only compositing nodes and is only used for compositing renders created by the original scene.

What I want to do is to specifically address an image node in the compositor node tree, and then assign an image to it. That's it. So, for me, pseudo-code would look something like:

set the context to the compositor node tree
set the filename of the image node "Fred" to "E:/myimage.jpg"


I've been referred to: How do I edit an image node in blender using python? but can not get it to work properly. How to load and assign an image to a specific image node in the compositor?
Load your image using the path, add or get the image node in the compositor, and then simply assign your image data in memory to the image input of the node.

import bpy
from pathlib import Path

img_path = r"C:\MyDirectory\MyPicture.png"
if Path(img_path).is_file():
    img = bpy.data.images.load(img_path, check_existing=True)  # Load the image in data

    node_tree = bpy.context.scene.node_tree  # Get the current scene's compositor node tree

    img_comp_node = node_tree.nodes.new(type='CompositorNodeImage') #  Create a new image node
    # img_comp_node = node_tree.nodes.get('MyImageNodeName')  # Use this if your node already exists
    if img_comp_node:
        img_comp_node.image = img
else:
    print(f"The file at {img_path} doesn't exist")


Read: Controling compositor by python