forked from GeneralUserModels/napsack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_dpi.py
More file actions
159 lines (134 loc) · 5.23 KB
/
check_dpi.py
File metadata and controls
159 lines (134 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""Quick script to check display DPI and scale factor using various methods."""
import sys
def check_screeninfo():
"""Check DPI using screeninfo library."""
print("=" * 50)
print("Method 1: screeninfo")
print("=" * 50)
try:
from screeninfo import get_monitors
for i, m in enumerate(get_monitors()):
print(f"\nMonitor {i}: {m.name}")
print(f" Resolution: {m.width} x {m.height} pixels")
print(f" Position: ({m.x}, {m.y})")
if m.width_mm and m.height_mm:
dpi_x = m.width / (m.width_mm / 25.4)
dpi_y = m.height / (m.height_mm / 25.4)
print(f" Physical size: {m.width_mm} x {m.height_mm} mm")
print(f" Calculated DPI: {dpi_x:.1f} x {dpi_y:.1f}")
else:
print(" Physical size: Not available")
except Exception as e:
print(f" Error: {e}")
def check_macos_quartz():
"""Check scale factor using macOS Quartz."""
print("\n" + "=" * 50)
print("Method 2: macOS Quartz (CGDisplay)")
print("=" * 50)
if sys.platform != "darwin":
print(" Skipped: Not on macOS")
return
try:
import Quartz
main_display = Quartz.CGMainDisplayID()
# Get pixel dimensions
pixel_width = Quartz.CGDisplayPixelsWide(main_display)
pixel_height = Quartz.CGDisplayPixelsHigh(main_display)
# Get display bounds (in points, which may differ from pixels on Retina)
bounds = Quartz.CGDisplayBounds(main_display)
point_width = bounds.size.width
point_height = bounds.size.height
scale_factor = pixel_width / point_width
print(f" Pixel dimensions: {pixel_width} x {pixel_height}")
print(f" Point dimensions: {point_width:.0f} x {point_height:.0f}")
print(f" Scale factor: {scale_factor:.1f}x")
print(f" Is Retina: {'Yes' if scale_factor > 1 else 'No'}")
except Exception as e:
print(f" Error: {e}")
def check_macos_appkit():
"""Check scale factor using macOS AppKit/NSScreen."""
print("\n" + "=" * 50)
print("Method 3: macOS AppKit (NSScreen)")
print("=" * 50)
if sys.platform != "darwin":
print(" Skipped: Not on macOS")
return
try:
from AppKit import NSScreen
for i, screen in enumerate(NSScreen.screens()):
backing_scale = screen.backingScaleFactor()
frame = screen.frame()
print(f"\nScreen {i}:")
print(f" Frame: {frame.size.width:.0f} x {frame.size.height:.0f} points")
print(f" Backing scale factor: {backing_scale}x")
print(f" Actual pixels: {frame.size.width * backing_scale:.0f} x {frame.size.height * backing_scale:.0f}")
except Exception as e:
print(f" Error: {e}")
def check_mss():
"""Check what mss captures (actual screenshot size)."""
print("\n" + "=" * 50)
print("Method 4: mss (actual screenshot capture)")
print("=" * 50)
try:
import mss
with mss.mss() as sct:
for i, monitor in enumerate(sct.monitors):
print(f"\nMonitor {i}: {monitor}")
if i > 0: # Skip the "all monitors" entry
# Capture a tiny portion to check actual pixel size
region = {
"left": monitor["left"],
"top": monitor["top"],
"width": 100,
"height": 100
}
img = sct.grab(region)
print(f" Requested 100x100, got: {img.width} x {img.height}")
if img.width != 100:
print(f" → Detected scale factor: {img.width / 100}x")
except Exception as e:
print(f" Error: {e}")
def recommend_scale():
"""Provide a recommended scale factor."""
print("\n" + "=" * 50)
print("RECOMMENDATION")
print("=" * 50)
scale = 1.0
# Try mss method first (most reliable for actual capture size)
try:
import mss
with mss.mss() as sct:
if len(sct.monitors) > 1:
monitor = sct.monitors[1]
region = {
"left": monitor["left"],
"top": monitor["top"],
"width": 100,
"height": 100
}
img = sct.grab(region)
if img.width != 100:
scale = img.width / 100
except:
pass
# Fallback to AppKit on macOS
if scale == 1.0 and sys.platform == "darwin":
try:
from AppKit import NSScreen
scale = NSScreen.mainScreen().backingScaleFactor()
except:
pass
print(f"\n Detected display scale: {scale}x")
print(f"\n To save at logical resolution (1:1 with display points):")
print(f" --scale {1/scale:.2f}")
print(f"\n To save at native resolution (full pixels):")
print(f" --scale 1.0")
if __name__ == "__main__":
print("\nDPI / Scale Factor Detection\n")
check_screeninfo()
check_macos_quartz()
check_macos_appkit()
check_mss()
recommend_scale()
print("\n")