automation: move image operations to own functions

Authored by indygreg.

Description

automation: move image operations to own functions

An upcoming commit will need this functionality with slightly different
values and it is enough code to not want to duplicate. Let's refactor
into standalone functions so it can be reused.

Differential Revision: https://phab.mercurial-scm.org/D6318

Details