Fill each object by average color behind their layer

Hello,

Does anyone know of any script that can fill multiple selected object with the average color behind their layer? Also, it can work even if it’s not the average color, just a color of a pixel from inside each and every object.

Something like this https://i.imgur.com/wE3h5u5.jpg

I need it to create a special mosaic, but the tiles are random in shape and size and are already drawn on top of the initial picture and there are over 100k objects/tiles that are needed to be filled.

I’ll apreciate any help with this!