updated array_to_image function