That's because a repeating field is (and should be) rarely used for this. A related table with one record for each image is more flexible and avoids this issue. It's pretty simple to use Import Records to copy this data from the repeating fields into such a table with the data in each repetition split into a separate record. This will then also solve your issue of images not being filled in from the first repetition onwards as any empty repetitions will not produce a record in such a new related table.
Without such a design change, you are looking at a fairly complex script that loops through the records, and on each record loops through the individual repetitions looking for empty repetitions and using a third loop to move the images from repetition to repetition when an empty rep is found.
Thank you for the prompt reply. You're right that does sound particularly convoluted, and I should be using portals for this instead.
Thanks, I'll make the design change.
Keep up the good work Phil, you've always got the solution to all my problems :)