Render scene with background jpg in python Fairly new to python and blender, so apologies if this is super basic. I've searched around and haven't found an answer.

I'd like to render some procedurally generated objects with a background image. In case it makes a difference, I'm using:



I don't need any interaction between the generated object and the background image so I've left a skeleton script I'm using to render below

import os
DIR ="/home/path/to/folder"
bg_file = os.path.join(DIR, "myimage.jpg")
bg_img = bpy.data.images.load(bg_file)
bpy.data.scenes['Scene'].render.filepath = os.path.join(DIR, "image")
bpy.context.scene.camera = bpy.data.objects['Camera']
bpy.ops.render.render(write_still=True)


I managed to find some scripts that seem to be trying to do what I want online, but none that I could get to work. e.g.

import bpy
filepath = "/home/path/to/folder"
img = bpy.data.images.load(filepath)

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        space_data = area.spaces.active
        bg = space_data.background_images.new()
        bg.image = img
        break

cam = bpy.data.cameras.new("Camera")
cam_ob = bpy.data.objects.new("Camera", cam)
bpy.context.scene.camera = cam_ob
bpy.context.scene.objects.link(cam_ob)
bpy.context.scene.objects.active = bpy.context.scene.objects["Camera"]


Any thoughts/tips would be greatly appreciated!
I tried the same thing above, but didn't work. What worked for me is with the textures. In this case, I think you also have to take care of the Texture fields. 


bpy.data.textures['Texture.001'].image=bg_img


It works if you need to Change just background

Also add this,


bpy.context.scene.world.use_sky_paper = True
Set also influence, horizontal


Sorry, I dont have enough credits to comment!