Convert pycairo ARGB32 surface to PIL RGB image

Table of Contents

Cairo is great for rendering stuff, but doesn't give you many options to export your data. The Python Image Library has good options to save stuff. The problem I ran into is that I have a ARGB32 pycairo surface, and PIL doesn't support ARGB32. It supports RGBA instead. After much experimentation, I came up with the following piece of code, using numpy to solve this problem: