pack img dump loop into nested functions