Simulating Screen Rotation with Python

What Is Screen Rotation?

Screen rotation automation allows a Python script to programmatically flip the display orientation — simulating the effect of a user manually rotating the screen via system shortcuts or display settings. This demo includes working solutions for macOS, Linux, and Windows using tools like displayplacer, xrandr, and rotatescreen.

Why Would You Use This?

It's mainly used for pranks or testing how applications behave in rotated environments. Note: These scripts rely on OS-specific utilities and may require additional tools or permissions. macOS and Linux require setup steps, and some Windows machines may block rotation commands depending on driver or system settings.

Python Rotation Script

This Python script provides screen rotation routines for macOS, Linux, and Windows. Follow the instructions in each section of the code for setup.


'''
For MacOS

1. Install the following in Homebrew:
    brew tap jakehilborn/jakehilborn
    brew install displayplacer
    
2. Rotate your primary display by finding its persistent ID:
    displayplacer list
    
    You should see something like the following:
    Persistent screen id: 12345678-ABCD-1234-ABCD-1234567890AB
    
3. Copy and paste that ID in the display_id part of your Python code
'''

import subprocess, time

DISPLAY_CMD_TEMPLATE = (
    'displayplacer "id:{id} res:1920x1080 hz:60 color_depth:8 scaling:on origin:(0,0) degree:{deg}"'
)

display_id = "F12C3A7E-B1C3-538D-DA06-F30A2911D4DD"  # your persistent ID

angles = [90, 180, 270, 0]
for _ in range(5):
    for angle in angles:
        cmd = DISPLAY_CMD_TEMPLATE.format(id=display_id, deg=angle)
        subprocess.run(cmd, shell=True)
        time.sleep(2)

'''
For Linux

1. Install the following:
    sudo apt install x11-xserver-utils  # Ubuntu/Debian

2. List displays:
    xrandr

3. Look for lines like:
    HDMI-1 connected ...
    eDP-1 connected ...
'''

import subprocess, time

display = "HDMI-1"  # Replace with your actual display name from `xrandr`
angles = ["left", "inverted", "right", "normal"]

for _ in range(5):
    for angle in angles:
        cmd = f"xrandr --output {display} --rotate {angle}"
        subprocess.run(cmd, shell=True)
        time.sleep(2)

'''
For Windows

1. Use the rotatescreen Python package
    pip install rotatescreen
'''

import rotatescreen, time

screen = rotatescreen.get_primary_display()
angles = [90, 180, 270, 0]

for _ in range(5):
    for angle in angles:
        screen.rotate_to(angle)
        time.sleep(2)
        

Tip: If your display becomes hard to use, wait 2–3 cycles for it to rotate back to normal automatically.

Compatibility & Caution

This script works best on systems with appropriate driver support and permissions. Do not use this on public or shared machines. Always test carefully and know how to restore display settings manually if needed.