Extracting image rotation from metadata

I’m trying to extract the rotation of images around the z-axis of the camera.
I want to rotate the image in the other direction to get a level horizon.

I’ve fetched the field computed_rotation using the API and converted the rotation vector to Euler angles using SciPy:

from scipy.spatial.transform import Rotation as R
rotation = R.from_rotvec(computed_rotation)
rotation = rotation.as_euler("xyz", degrees=True)[2]

The resulting rotation is close to ±180 degrees for many images, turning the image upside down.

Can anyone help me on what I’m doing wrong?

Thanks in advance!

Am I right, you want to fix the horizon in 360 images ?

If so, I use Adobe Affinity, to straighten horizon

Open Ctrl+O

Layer → Live Projection → Equirectangular Projection

Use Heading to turn, Use Straighten to correct Horizon

exit Projection, then save

Thank you for the reply, but I meant something different.
I want to rotate a perspective image around it’s centre to make the horizon horizontal.

Take this image for example (key: 508132680364463):

The image is tilted to the left. The lantern on the left and the buildings in the background aren’t vertical.

If I extract the rotation from the API, as described in my original post, I get 173.16 degrees resulting in an upside down image:

The result I want to get should look like this image, which was rotated by 4.5 degrees manually: