Changing the image in the Image texture node via python

Hi! Does anybody know how to load a new image from disk in a defined image texture node?

Suppose, I have an image texture node called main image, and I want to change the loaded image with a new one that I load from disk.
I was able to reach the wanted node with the code, but when I need to place in the new image I can’t get the code to do so.

obj.material_slots[0].material.node_tree.nodes[“main image”]. and then?

suppose the image path is “C:\images\image2.jpg”


1 Like

I tried this way, that is opening a new image with the operator, and then assign it to the image propriety of the node, but it tells me that it isn’t an image type. What is wrong?

image = = ‘C:\images\image2.jpg’)
obj.material_slots[0].material.node_tree.nodes[“main image”].image = image

EDIT: if I run only the line with the open operator, the image is correctly loaded in blender. In fact, I find it in the images list of the node. So I just need the command to select that image for that node.

Ok, I found the solution.

First, it’s needed to create the new image, but using the load function, not open, in the images list.
new_img = = “C:\images\image2.jpg”)

in this way a correct image datablock is created.

then you can assign new_img to the node propriety
obj.material_slots[0].material.node_tree.nodes[“main image”].image = new_img

if you instead want to set an image from the list that is already in list, just call it from the data module by its name

obj.material_slots[0].material.node_tree.nodes[“main image”].image =[“image2.jpg”]


Thanks a lot

I need some help.
I have an image I want to change some texture of that image with given texture. The given texture is also image. Please help me with some code.