refactor pimg tool