modified saveImage to save an encoded version of the image