Programmatically update node image field (using URI of the image) and get URL in twig I am trying to update a node image field programmatically. I currently loading the node like this:

$node = Node::Load(1);


have the URI if the image that I want to set. I have tried this but no luck:

$node-&gt;set('field_image_head', array('#uri' =&gt; $imageUri));



How do I now update the node image with an image URI? 
The twig will need to get the URL from the image field like so {{ file_url(node.field_image_head.entity.fileuri}}
You have to create a file for the image first before you can add it to the node.

use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;

  $node = Node::load(1);
  $uri = 'public://image.jpg';

  // check first if the file exists for the uri    
  $files = \Drupal::entityTypeManager()
    -&gt;getStorage('file')
    -&gt;loadByProperties(['uri' =&gt; $uri]);
  $file = reset($files);

  // if not create a file
  if (!$file) {
    $file = File::create([
      'uri' =&gt; $uri,
    ]);
    $file-&gt;save();
  }

  $node-&gt;field_image[] = [
    'target_id' =&gt; $file-&gt;id(),
    'alt' =&gt; 'Alt text',
    'title' =&gt; 'Title',
  ];

  $node-&gt;save();