如何使用命令行获取显示器分辨率?

我想找一款适合我的分辨率的壁纸。如何通过在命令行中输入命令来获取分辨率?
10个回答

这个答案中摘录:
xdpyinfo | grep dimensions

或者只获取分辨率:
xdpyinfo | awk '/dimensions/{print $2}'

或者

xdpyinfo  | grep -oP 'dimensions:\s+\K\S+'

18它适用于单个显示器设置,但对于两个显示器,它会将两个尺寸相加。对我来说,我的两个屏幕的分辨率是:3520x1200像素。 - Sylvain Pineau
4好观点。另一方面,如果他要寻找一张能够覆盖所有显示器的单个壁纸,这仍然是有用的。 - aguslr
确实,你说得对,+1 ;) - Sylvain Pineau
@aguslr那样做有什么意义呢?拥有两台2000x1000分辨率的显示器,使用4000x2000的壁纸图片有什么用处呢? - Jos
4@Jos,我明白这个命令会返回4000x1000,也就是将两个显示器并排放置。例如,Sylvain有两个显示器(1600x900和1920x1200),他得到的是3520x1200。 - aguslr
1@aguslr 哦,是的,你可能是对的。我太过于字面理解他的“它总结了两个维度”。 - Jos
@Jos 对于造成的混淆,我很抱歉。如果需要的话,我可以删除我的第一条评论。 - Sylvain Pineau
它显示的是监视器还是GPU的分辨率? - Roby Sottini

我会只使用 xrandr
$ xrandr 
Screen 0: minimum 320 x 200, current 3520 x 1200, maximum 32767 x 32767
LVDS1 connected 1600x900+1920+0 (normal left inverted right x axis y axis) 310mm x 174mm
   1600x900       60.0*+
   1440x900       59.9  
   1360x768       59.8     60.0  
   1152x864       60.0  
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
DP1 connected primary 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
   1920x1200      60.0*+
   1920x1080      60.0     50.0     59.9     24.0     24.0  
   1920x1080i     60.1     50.0     60.0  
   1600x1200      60.0  
   1280x1024      75.0     60.0  
   1152x864       75.0  
   1280x720       60.0     50.0     59.9  
   1024x768       75.1     60.0  
   800x600        75.0     60.3  
   720x576        50.0  
   720x480        60.0     59.9  
   640x480        75.0     60.0     59.9  
   720x400        70.1  
HDMI2 disconnected (normal left inverted right x axis y axis)
HDMI3 disconnected (normal left inverted right x axis y axis)
DP2 disconnected (normal left inverted right x axis y axis)
DP3 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

这里有两个屏幕,分辨率分别是:
  • 1600x900(笔记本电脑)
  • 1920x1200(显示器)

如果你只想获取主要显示器的分辨率,你也可以使用这个Python一行代码:

$ python3 -c 'from gi.repository import Gdk; screen=Gdk.Screen.get_default(); \
geo = screen.get_monitor_geometry(screen.get_primary_monitor()); \
print(geo.width, "x", geo.height)'
1920 x 1200

要获取扩展桌面的分辨率(用于多监视器设置):

$ python3 -c 'from gi.repository import Gdk; screen=Gdk.Screen.get_default(); \
print(screen.get_width(), "x", screen.get_height())'
3520 x 1200

xrandr + vesa = 不工作。 - Joshua
1只返回正在使用的显示器的分辨率:xrandr | grep " connected\|\*" - Pablo Bianchi

请求是关于“分辨率”的。这由给出。
xdpyinfo | grep resolution

6通常,人们使用分辨率来指代尺寸。与尺寸相比,DPI并不是那么重要的关注点。 - muru

您还可以使用以下方法:
 xrandr | grep ' connected'

我机器上的输出示例:

LVDS connected primary 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm

在没有X的树莓派上,我可以通过运行以下命令获取屏幕分辨率:

fbset -s

太棒了!正是我在寻找的东西。 - August Karlstrom

您可以通过以下方式获取当前屏幕的分辨率:
  1. 运行以下命令获取X分辨率:

    X=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)
    
  2. 运行以下命令获取Y分辨率:

    Y=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)
    
  3. 运行以下命令输出X和Y分辨率:

    echo "$X"x"$Y"
    

这样做应该是可能的,而且不需要使用 awk

另一种替代方法是:

你可以通过终端打开你喜欢的网站,它会显示你的屏幕分辨率。

firefox https://de.piliapp.com/what-is-my/screen-resolution/

你也可以通过使用https://www.brow.sh/在你的终端上显示网页。
如果愿意,你可以通过在终端中包含适当的Python页面抓取工具来抓取这个页面,并将输出导出到一个终端变量中。

另一个解决方案是使用xprop:
$ xprop -notype -len 16 -root _NET_DESKTOP_GEOMETRY | cut -c 25-
11520, 1080

如果有人在意的话,对于我的机器来说,它比使用xdpyinfo进行grep稍微快一点。
或者,如果你只关心宽度:
$ xprop -notype -len 8 -root _NET_DESKTOP_GEOMETRY | cut -c 25-
11520

仅在通过Xinerama组织的多个显示器上进行了测试。

如果一个人有两个连接的显示器,这似乎会给出它们尺寸的总和。 - Bach
1当显示器关闭时,xprop给出了错误的尺寸。 - undefined

就这么说吧,当使用多个连接的显示器和/或TwinView的偏移时,xdpyinfo将以它们配置的方式提供整个显示器集合的分辨率。如果您需要单个监视器或连接到其中一个显示端口的监视器的分辨率,则需要使用xrandr。然而,即使在该配置下,xrandr也可能不可靠并且不显示分辨率。请参考我X窗口配置文件中的这个示例条目:
Option "MetaModes" "DP-1: 1440x900 +0+0, DP-3: 1440x900 +1568+0, DP-5: 1440x900 +3136+0"

xrandr的输出如下所示:
DVI-D-0 disconnected primary (normal left inverted right x axis y axis)
HDMI-0 disconnected (normal left inverted right x axis y axis)
DP-0 disconnected (normal left inverted right x axis y axis)
DP-1 connected 1440x900+0+0 (normal left inverted right x axis y axis) 410mm x 256mm
   1440x900      59.89*+
   1280x1024     60.02
   1280x960      60.00
   1280x800      59.81
   1280x720      60.00
   1152x864      75.00
   1024x768      70.07    60.00
   800x600       75.00    60.32    56.25
   640x480       75.00    72.81    59.94
DP-2 disconnected (normal left inverted right x axis y axis)
DP-3 connected (normal left inverted right x axis y axis)
   1440x900      59.89 +  74.98
   1280x1024     60.02
   1280x960      60.00
   1280x800      59.81
   1280x720      60.00
   1152x864      75.00
   1024x768      70.07    60.00
   800x600       75.00    60.32    56.25
   640x480       75.00    72.81    59.94
DP-4 disconnected (normal left inverted right x axis y axis)
DP-5 connected 1440x900+1568+0 (normal left inverted right x axis y axis) 410mm x 256mm
   1440x900      59.89*+
   1280x1024     60.02
   1280x960      60.00
   1280x800      59.81
   1280x720      60.00
   1152x864      75.00
   1024x768      70.07    60.00
   800x600       75.00    60.32    56.25
   640x480       75.00    72.81    59.94

你可以看到DP-3在不显示一个grep for "connected"的分辨率行。所以我找到的最好、最一致和可靠的命令来识别任何连接显示器的分辨率是:
/usr/bin/xrandr --query|/usr/bin/grep -A 1 connected|grep -v connected

这是由它产生的内容:
   1440x900      59.89*+
--
   1440x900      59.89*+  74.98
--
   1440x900      59.89*+

在那个时候,挑选出不同的分辨率或者只搜索一个端口是相当简单的事情。

你还可以执行 xrandr | grep '\bconnected\b'
干杯!

这会输出很多东西,包括显示器的分辨率... - Eddy763

你可以通过bash命令来获取显示器的分辨率。
read x y < <(xrandr --current | grep -oP '\d+x\d+' | tr x ' ')
echo $x
echo $y

@Downvoter,这在我的系统上运行得很好。 - Eddy763