Texture not being saved in python scripted output

I am trying to write a Python script to render an object on a preexisting image. If I press the render button in the Blender GUI everything works perfectly but if I try to save the output with Python commands the background is just grey. What am I doing wrong?

Here is an example bit of code.

import bpy
import math
import mathutils

bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.mesh.primitive_plane_add(radius=1, view_align=False, 
enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, 
False, False, False, False, False, False, False, False, False, False, False, 
False, False, False, False, False))
bpy.data.objects['Plane'].name = "Ground"
bpy.data.objects['Ground'].scale = ((118.518,87.3072,1))

ob = bpy.context.active_object
# Get material
mat = bpy.data.materials.get("Screen")
if mat is None:
    # create material
    mat = bpy.data.materials.new(name="Screen")
# Assign it to object
if ob.data.materials:
    # assign to 1st material slot
    ob.data.materials[0] = mat
    # no slots

bpy.data.materials['Screen'].use_nodes = True
nodes = mat.node_tree.nodes
# clear all nodes to start clean
for node in nodes:

inp = bpy.data.materials['Screen'].node_tree.nodes['Diffuse BSDF'].inputs['Color']
outp = bpy.data.materials['Screen'].node_tree.nodes['Image Texture'].outputs['Color']
inp = bpy.data.materials['Screen'].node_tree.nodes['Material Output'].inputs['Surface']
outp = bpy.data.materials['Screen'].node_tree.nodes['Diffuse BSDF'].outputs['BSDF']
croppedImage = bpy.data.images.load('C:\\Cropped.png')
bpy.data.materials['Screen'].node_tree.nodes['Image Texture'].image = croppedImage
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action = 'SELECT')

bpy.data.lamps['Lamp'].sky.use_sky = True
bpy.data.objects['Lamp'].location = (0,0,40)
bpy.data.objects['Lamp'].rotation_euler = 
mathutils.Euler((0.0,0.523598775598,-1.57079632679), 'XYZ')
bpy.data.worlds['World'].horizon_color = (1, 1, 1)
bpy.data.scenes['Scene'].render.resolution_y = 480
bpy.data.scenes['Scene'].render.resolution_x = 640
bpy.data.scenes['Scene'].render.resolution_percentage = 100
bpy.data.scenes['Scene'].cycles.film_transparent = True

bpy.data.scenes['Scene'].render.filepath = 'C:\\Test_1.png'
bpy.ops.render.render( write_still=True, use_viewport=True, scene="Scene", layer="RenderLayer")

The script produces this output:

If I press the tender button in the GUI I get a properly rendered background with an object.

Hi, I’ve used my privilege as a forum regular to move this thread to the python support section of this site. Hopefully one of the python coders who check this portion of the forum will be able to answer you. If you still don’t get any responses, then try asking this at the blender developer forum (https://devtalk.blender.org/).