Q:

Only mirror screen rotation - Python

I have problem with programmatic monitor rotation
import win32api as win32
import win32con
MY_SCREEN_NUMBER = 1
device = win32.EnumDisplayDevices(None,MY_SCREEN_NUMBER)
dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS)
dm.DisplayOrientation = win32con.DMDO_180
dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION
win32.ChangeDisplaySettingsEx(device.DeviceName,dm)

Code rotating screen only in opposite direction, for example 

if screen in win32con.DMDO_DEFAULT it can only rotate it in win32con.DMDO_180,
if screen in win32con.DMDO_90 only rotates in win32con.DMDO_270
otherwise it gives me -2 (DISP_CHANGE_BADMODE) error code

But i can successfully rotate it with windows graphical screen params settings
How do I rotate the screen correctly?

A:

def rotateTo(degree):
   import win32api as win32
   import win32con
   MY_SCREEN_NUMBER = 1
   device = win32.EnumDisplayDevices(None,MY_SCREEN_NUMBER)
   dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS)
   newDisplayOrientationInWin32Format = degree/90
   currentDisplayOrientationInWin32Format = dm.DisplayOrientation
   if(newDisplayOrientationInWin32Format + currentDisplayOrientationInWin32Format) % 2 == 1 : #test the new resolution on the perpendicular to the previous
       tmp = dm.PelsHeight
       dm.PelsHeight=dm.PelsWidth
       dm.PelsWidth=tmp
   dm.DisplayOrientation = newDisplayOrientationInWin32Format
   return win32.ChangeDisplaySettingsEx(device.DeviceName,dm)

I checked https://msdn.microsoft.com/en-us/library/ms812499.aspx and found C example, i need to swap height and width, but they don't test the new resolution on the perpendicular to the previous. I updated code with this test and it start working in any possible screen orientation for me.

