Optimise image and gradient