Filter the output

how can I filter the output of an element, e.g. the gallery - I’m nor talking about filtering the output on the front-end but a way to change the html output before it is on the page.