Automatically detect image flip and reverse

thanks a lot for your inspiring post! I struggling actually also with an rotation problem a bit different you have done:

I have a binary image with a white rectangle. The image dimensions are sufficient so rotating the image up to 90° doesnt cut off anything. So far so good!

At the starting point (0° rotation) I count the white pixels.
Then I rotate the image using scipy.ndimage.rotate with reshape=False from 0°:1°:90° counting the white pixels of the rotated image and estimating the difference regarding no rotation.

What do I get: for some angles, the rotated rectangle has more and sometimes less white pixels than the rectangle without any rotation.
Do you know am algorithm or any hint to get a pixelinvariant rotation?? I´m struggling since days without any idea