How do I add a variable for a Twig template in my preprocess function?
I finally solved the problem, it turns out the problem was that I tried to add the variables in the wrong preprocessor. Editing the #theme works there, but to add variables I needed to preprocess the theme I set there:

function template_preprocess_imagegallery_format(&amp;$vars) {
  template_preprocess_image_formatter($vars);
  $vars['image']['#theme'] = 'igimage';
}

function template_preprocess_igimage(&amp;$vars) {
    $vars['thumbnail'] = ImageStyle::load('thumbnail')-&gt;buildUrl($vars['uri']);
    $vars['fullimage'] = file_create_url($vars['uri']);
}