The Variant image set feature lets you link specific images to a variant so that only those images appear when the variant is selected on the product page.
This feature is primarily designed for color variants, but it will work with any option name.
Add alt text to images
Images are assigned to variants based on the alt text applied to each product image.
Adding alt text to images - Shopify Help Center
Use the following syntax for the alt text:
#option-name_variant-name
option-name: the product’s option name (e.g., Color)
variant-name: the specific variant value (e.g., Red)
Be sure to follow this format exactly. Any mistakes will prevent the image from being assigned to the correct group. Here is an example:
Alt text is commonly used for SEO and accessibility, and we recommend continuing to use it that way while also using this feature. Text added before the # will display normally on the page, and text after the # is used by the variant image set feature.
When this feature is enabled, every image must be part of a variant image set. Images that aren’t linked to a variant (or don’t have alt text added) will not appear. If you want the same image to appear for multiple variants, upload the image multiple times and add the matching alt text for each variant.
Limitation
This feature only works when Thumbnails below media or Thumbnails beside media is selected in the Media gallery layout setting of the Product template.