Use node custom field to choose my posts background
Why use a preprocess function ? Instead use a template =).

Here is an example of how I achieve this functionality for the content type "Article" + the image field.

node--article.html.twig

  &lt;header class="main-header post-head " style="background-image: url({{ file_url(node.field_image.entity.fileuri)}})"&gt; &lt;/header&gt;


I added the image on the header on top of my template and use the file_url to get the image from the field_image.

  {{ file_url(node.field_image.entity.fileuri)}}


This will allow you to get the image URL.

Few wrappers + classes to make it this image as background image. And every node will have a custom image background.