@@ -61,7 +61,17 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
61
61
left , top , right , bottom = bbox
62
62
im = im .crop ((left - x0 , top - y0 , right - x0 , bottom - y0 ))
63
63
return im
64
- elif shutil .which ("gnome-screenshot" ):
64
+ try :
65
+ if not Image .core .HAVE_XCB :
66
+ msg = "Pillow was built without XCB support"
67
+ raise OSError (msg )
68
+ size , data = Image .core .grabscreen_x11 (xdisplay )
69
+ except OSError :
70
+ if (
71
+ xdisplay is None
72
+ and sys .platform not in ("darwin" , "win32" )
73
+ and shutil .which ("gnome-screenshot" )
74
+ ):
65
75
fh , filepath = tempfile .mkstemp (".png" )
66
76
os .close (fh )
67
77
subprocess .call (["gnome-screenshot" , "-f" , filepath ])
@@ -73,15 +83,13 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
73
83
im .close ()
74
84
return im_cropped
75
85
return im
76
- # use xdisplay=None for default display on non-win32/macOS systems
77
- if not Image .core .HAVE_XCB :
78
- msg = "Pillow was built without XCB support"
79
- raise OSError (msg )
80
- size , data = Image .core .grabscreen_x11 (xdisplay )
81
- im = Image .frombytes ("RGB" , size , data , "raw" , "BGRX" , size [0 ] * 4 , 1 )
82
- if bbox :
83
- im = im .crop (bbox )
84
- return im
86
+ else :
87
+ raise
88
+ else :
89
+ im = Image .frombytes ("RGB" , size , data , "raw" , "BGRX" , size [0 ] * 4 , 1 )
90
+ if bbox :
91
+ im = im .crop (bbox )
92
+ return im
85
93
86
94
87
95
def grabclipboard ():
0 commit comments