An easy coding, though not super efficient, way to do this is to use the inclusion-exclusion principal, which in its simplest form says that for any two (measureable) subsets A and B
area( A union B) = area( A) + area( B) - area( A inter B)
We are only concerned with unions of axis aligned rectangles, and the intersection of a rectangle with a union of rectangles is itself a union of rectangles. So to find the area of the union of rectangles R1, .. Rn we get the recursive function (in pseudo code as I don't know python, sorry)
area ( R1 union R2 union .. Rn )
= rectarea( R1) + area( R2 union .. Rn)
- area( (R1 inter R2) union (R1 inter R3) .. (R1 inter Rn)
If we know xmin,xmax,ymin,ymax for a rectangle then its area is
(xmax-xmin)*(ymax-ymin)
If we have two rectangles R and S, their intersection is
(RinterS).xmin = max( R.xmin, S.xmin)
(RinterS).xmax = min( R.xmax, S.xmax)
(RinterS).ymin = max( R.ymin, S.ymin)
(RinterS).ymax = min( R.ymax, S.ymax)
When computing (R1 inter R2) etc you have to do that in a copy of the inputs not the inputs themselves (as the inputs may be used by a recursive call that comes later). Its worth eliminating the empty resulting rectangles (ie those with xmin>=xmax or ymin>=ymax). If you
don't eliminate them you have to ensure that their areas are computed as 0.
I have C code that implements this, which I'll post if you're interested.