Moves encoding of image into separate function