# I can't load an image from a script

I'm trying to use Blender's mesh displacement from a script to create stl's. Once this works, I'll run blender with --background to create the stl files from the command line.

I start Blender with:

blender ~/3D/Models/4x4UVSquare.blend -d -P ./test.py


the script is:

import bpy

bpy.ops.image.open(filepath="junk.jpg", \
directory="/Users/me/3D/Art Work/", \
files=[{"name":"junk.jpg", "name":"junk.jpg"}], \
relative_path=True, show_multiview=False \
)
bpy.data.textures['displaceImage'].image = bpy.data.images['junk.jpg']


I put in the ".reload()" hoping it would help. It doesn't. Blender starts up, loads the .blend file, loads the image and assigns the image to the texture. When the UI comes up and displays the scene, there's no displacement. In the UI Image area under Texture, the filename is correct, but a little message reads "Can't load image".

When I hit the "reload" icon, the image reloads, the displacement appears and all it well.

Why do I have to reload the image at all? Why doesn't image.reload() work in the script?

To load an image from disk doesn't require bpy.ops. You could use:

bpy.data.images.load(filepath, check_existing=False)
# Load a new image into the main database

# bpy.data.images['some_image.png']

# if an image exists by that name already, you could set the name
bpy.data.images['some_image.png.001'].name = 'some_image.png'

# check_exist=True will reuse an existing image with that name

bpy.ops.* are triggered each time you interact in some way with the UI (buttons, shortcuts..). Often the UI triggers a bpy.ops.whatever with information about which window the event occurred in - this is important because you could have two UV image editors open, or two TextEditors (just an example). Therefor sometimes just calling the ops isn't enough, and some ops even expect a UI which isn't present in background mode.