Skip to content

Instantly share code, notes, and snippets.

@nicksherman
Forked from justvanrossum/screen_info.py
Last active November 12, 2023 17:37
Show Gist options
  • Select an option

  • Save nicksherman/33f400e8577f93ab6ecdecdd4306985a to your computer and use it in GitHub Desktop.

Select an option

Save nicksherman/33f400e8577f93ab6ecdecdd4306985a to your computer and use it in GitHub Desktop.
macOS/Python: Get dimension info about active screens
from AppKit import NSScreen, NSDeviceSize, NSDeviceResolution
from Quartz import CGDisplayScreenSize
for i, screen in enumerate(NSScreen.screens(), 1):
description = screen.deviceDescription()
pw, ph = description[NSDeviceSize].sizeValue()
rx, ry = description[NSDeviceResolution].sizeValue()
mmw, mmh = CGDisplayScreenSize(description["NSScreenNumber"])
scaleFactor = screen.backingScaleFactor()
pw *= scaleFactor
ph *= scaleFactor
print(f"Display #{i}: {mmw:.1f} × {mmh:.1f} mm; {pw:.0f} × {ph:.0f} pixels; reported virtual resolution = {rx:.0f} × {ry:.0f} ppi; actual physical pixel density = {pw/mmw*25.4} × {ph/mmh*25.4} ppi")
@nicksherman
Copy link
Copy Markdown
Author

nicksherman commented Jun 6, 2019

Example output:

Display #1: 643.8 × 402.4 mm; 2560 × 1600 pixels; reported virtual resolution = 72 × 72 ppi; actual physical pixel density = 101.00000151686781 × 101.00000151686783 ppi
Display #2: 331.0 × 206.9 mm; 2880 × 1800 pixels; reported virtual resolution = 144 × 144 ppi; actual physical pixel density = 221.00000331908706 × 221.00000331908706 ppi

@JC3
Copy link
Copy Markdown

JC3 commented Nov 12, 2023

Thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment