How do I programmatically add an image while programmatically creating nodes?
Let suppose your image fields in field_body_images ,

first load your node by node_load
and record your image in file tables and add it to your node image field
I hope this sample help you to achieve :

$n=  node_load($nid);
$file = new stdClass();      
$file-&gt;filename =$file_name;
$file-&gt;filemime =file_get_mimetype($localimagepath.$file_name);
$file-&gt;filesize = @filesize(file_create_path($localimagepath.$file_name));
$file-&gt;uid = $user-&gt;uid;
$file-&gt;status = 1;
$file-&gt;timestamp = time();
$file-&gt;list=1;
$file-&gt;data=array('alt'=&gt;'','title'=&gt;$n-&gt;title);
drupal_write_record('files', $file);
$record-&gt;fid=$file-&gt;fid;
$n-&gt;field_body_images[]=(array)$file;
node_save($n);