'invalid render array key' error when using an associated array for a twig variable
Twig doesn't understand what you put in the keys image_style and image_path. Twig can handle only arrays when the children are renderable. And this means, they have to contain a '#type' or '#theme' key with a valid render element or theme name. For image styles use the theme image_style:

$variables['photos'] = [];
foreach ($images as $image) {
  $variables['photos'][] = [
    '#theme' =&gt; 'image_style',
    '#style_name' =&gt; 'photos',
    '#uri' =&gt; $image-&gt;entity-&gt;getFileUri(),
  ];
}


Then Twig can render the images like their are supposed to be rendered without the need for custom php code.