Is it possible to add an image to a node programmatically?
Here's one extra bit that tripped me up for a while: this will attach the image to the node, and if you're adding the image then you're okay. However, if you're updating an image, and you care about displaying it on a page, then one extra step is needed before calling node_save():
image_path_flush($node->field_image['und'][0]['uri']);
This will regenerate all of that image's styles.