The following code will help you to save an image in a new node.
use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
// Create file object from remote URL.
$image = file_get_contents(‘https://www.awaysomearticle.com/wp-content/uploads/2020/09/how-can-Render-Custom-Blocks-Programmatically-in-Drupal-8.png’);
$file = file_save_data($image, ‘public://how-can-Render-Custom-Blocks-Programmatically-in-Drupal-8.png’, FILE_EXISTS_REPLACE);
// Create node object with attached file.
$node = Node::create([
‘type’ => ‘article’,
‘title’ => ‘How can I programmatically create a node?’,
‘field_image’ => [
‘target_id’ => $file->id(),
‘alt’ => ‘How can I programmatically create a node?’,
‘title’ => ‘How can I programmatically create a node?’
],
]);
$node->save();