This should be pretty easy to achieve by adding a “data” attribute on the images that you need to match.
That way you can have a different image source and still match things up. For example if you had:
<img src="car.jpg" /> <!-- A car! -->
<img src="road.jpg" /> <!-- where do cars drive? A road! -->
You could relate these two by adding a data-attribute:
<img data-group="cars" src="car.jpg" /> <!-- A car! -->
<img data-group="cars" src="road.jpg" /> <!-- where do cars drive? A road! -->
<!-- another example -->
<img data-group="garden" src="flower.jpg" /> <!-- A flower! -->
<img data-group="garden" src="garden.jpg" /> <!-- where do flowers grow? A garden! -->
So instead of checking the SRC you could check the data-group.