@@ -149,29 +149,37 @@ def grabclipboard():
149149 session_type = None
150150
151151 if shutil .which ("wl-paste" ) and session_type in ("wayland" , None ):
152- output = subprocess .check_output (["wl-paste" , "-l" ]).decode ()
153- mimetypes = output .splitlines ()
154- if "image/png" in mimetypes :
155- mimetype = "image/png"
156- elif mimetypes :
157- mimetype = mimetypes [0 ]
158- else :
159- mimetype = None
160-
161- args = ["wl-paste" ]
162- if mimetype :
163- args .extend (["-t" , mimetype ])
152+ args = ["wl-paste" , "-t" , "image" ]
164153 elif shutil .which ("xclip" ) and session_type in ("x11" , None ):
165154 args = ["xclip" , "-selection" , "clipboard" , "-t" , "image/png" , "-o" ]
166155 else :
167156 msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"
168157 raise NotImplementedError (msg )
169158
170159 p = subprocess .run (args , capture_output = True )
171- err = p .stderr
172- if err :
173- msg = f"{ args [0 ]} error: { err .strip ().decode ()} "
160+ if p .returncode != 0 :
161+ err = p .stderr
162+ for silent_error in [
163+ # wl-paste, when the clipboard is empty
164+ b"Nothing is copied" ,
165+ # Ubuntu/Debian wl-paste, when the clipboard is empty
166+ b"No selection" ,
167+ # Ubuntu/Debian wl-paste, when an image isn't available
168+ b"No suitable type of content copied" ,
169+ # wl-paste or Ubuntu/Debian xclip, when an image isn't available
170+ b" not available" ,
171+ # xclip, when an image isn't available
172+ b"cannot convert " ,
173+ # xclip, when the clipboard isn't initialized
174+ b"xclip: Error: There is no owner for the " ,
175+ ]:
176+ if silent_error in err :
177+ return None
178+ msg = f"{ args [0 ]} error"
179+ if err :
180+ msg += f": { err .strip ().decode ()} "
174181 raise ChildProcessError (msg )
182+
175183 data = io .BytesIO (p .stdout )
176184 im = Image .open (data )
177185 im .load ()
0 commit comments