Improve the performance of ImageWriter._get_all_colours()