phunsukwangdu
phunsukwangdu

Reputation: 412

Screen rotation in windows with python

I am trying to write a python script to rotate screen in Windows.
I have clues of doing it with Win32api.
What are the other possibilities or commands to achieve so(Win32api included).

Upvotes: 1

Views: 6579

Answers (4)

chandima
chandima

Reputation: 141

you can simply use rotate-screen library to do screen rotations. (Only support for Windows right now)

import rotatescreen
screen = rotatescreen.get_primary_display()
screen.rotate_to(90) # rotate to 90 degrees

Upvotes: 2

sejal goyal
sejal goyal

Reputation: 23

you can use below code for screen rotation in any angle, I changed the code given by Mxl above.

import win32api as win32
import win32con
import sys
import re
x = 0
args=sys.argv[1].lower()
rotation_val=0
m = re.search("(?<=^-rotate=)\S+", args)    # Use non-white character wildcard instead of d decimal
if (m != None):
    print m.group(0)
    if ((m.group(0) == "180")):
        rotation_val=win32con.DMDO_180
    elif((m.group(0) == "90")):
        rotation_val=win32con.DMDO_270
    elif ((m.group(0) == "270")):   
        rotation_val=win32con.DMDO_90
    else:
        rotation_val=win32con.DMDO_DEFAULT

device = win32.EnumDisplayDevices(None,x)
dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS)
if((dm.DisplayOrientation + rotation_val)%2==1):
    dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth   
dm.DisplayOrientation = rotation_val

win32.ChangeDisplaySettingsEx(device.DeviceName,dm)

for running this script you will need to give the following command:-

filename.py -rotate=180
filename.py -rotate=0
filename.py -rotate=90
filename.py -rotate=270

Upvotes: 2

Atto
Atto

Reputation: 162

If you have the rotate shortcut active in windows (CTRL+ALT+ARROW KEY) you can use pyautogui.hotkey function.

Upvotes: 0

mxl
mxl

Reputation: 69

This is (a sligtly modified version of) the code that worked for me from the answer given above prvided by lbenini. Possible screen rotation values are win32con.DMDO_DEFAULT (0°), win32con.DMDO_90, win32con.DMDO_180 and win32con.DMDO_270 (one can obtain a list by typing help(win32con))

import win32api as win32
import win32con

def printAllScreen():
    i = 0
    while True:
        try:
            device = win32.EnumDisplayDevices(None,i);
            print("[%d] %s (%s)"%(i,device.DeviceString,device.DeviceName));
            i = i+1;
        except:
            break;
    return i

screen_count=printAllScreen()
x = int(input("\nEnter a display number [0-%d]: "%screen_count))


device = win32.EnumDisplayDevices(None,x);
print("Rotate device %s (%s)"%(device.DeviceString,device.DeviceName));

dm = win32.EnumDisplaySettings(device.DeviceName,win32con.ENUM_CURRENT_SETTINGS)
dm.DisplayOrientation = win32con.DMDO_180
dm.PelsWidth, dm.PelsHeight = dm.PelsHeight, dm.PelsWidth
dm.Fields = dm.Fields & win32con.DM_DISPLAYORIENTATION
win32.ChangeDisplaySettingsEx(device.DeviceName,dm)

Upvotes: 0

Related Questions