Improved image save function

Improved image save function