-
-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplified Linux clipboard support #38
Comments
Thanks for letting me know about the Pillow update. I feel like it's not worth it to keep the separate patched implementation so I might just add the temporary logging patch like you proposed and use the official release. Just one correction:
This is not exactly true, pyperclip is used only for returning the text, not for grabbing the image |
I see; I misunderstood the purpose of that code. I also figured out that one can use clipnotify to block until the clipboard is updated, instead of polling. Replacing time.sleep(delay_secs) with has_clipnotify = os.system("which clipnotify > /dev/null") == 0
...
# on linux with the clipnotify package
if sys.platform == "linux" and has_clipnotify:
# block until clipboard updates
with subprocess.Popen(
["clipnotify", "-s", "clipboard"],
stdout=subprocess.PIPE,
) as process:
process.stdout.read() # type: ignore
# poll in a platform-agonistic manner
else:
time.sleep(delay_secs) is all that is necessary. The solution is Linux and X11 specific however, and requires |
Addressed by #44. |
According to the README,
Checking the logic, there's two distinct ways
manga_ocr
handles clipboard:On Wayland, the pyperclip library is used to get clipboard contents
manga-ocr/manga_ocr/run.py
Line 72 in b0cac91
while on Windows and MacOS it's through Pillow's
PIL.ImageGrab.grabclipboard
manga-ocr/manga_ocr/run.py
Line 93 in b0cac91
Although the documentation for
grabclipboard()
saysit's undocumented but the Pillow source shows that Linux support was added in 9.4.0 (using
wl-paste
andxclip
).Thus one can change the existing code from
manga-ocr/manga_ocr/run.py
Lines 66 to 86 in b0cac91
to
and preserve support for Wayland while adding support for Linux systems running X.
This requires
Pillow>=9.4.0
andpyperclip
can probably be removed.(see master...stephen-huan:manga-ocr:linux-clipboard for an implementation)
If neither
wl-paste
norclip
are available, theNotImplementedError
should propagate through the try catch.Note that the implementation of
grabclipboard()
on Linux is currently a bit wonky, see python-pillow/Pillow#7147.In particular the current implementation will (a) generate a lot of unnecessary temporary files and (b) raise either
ChildProcessError
fromxclip
reportingError: target image/png not available
orUnidentifiedImageError
from when Pillow tries to parse non-image data, e.g. plaintext when the other operating systems returnNone
on invalid data.It may be worthwhile to temporarily patch
to
since the default level of
loguru
is debug, usingtrace
for the parse errors that occur everydelay_secs
prevents the user from being spammed when running with the default commandmanga_ocr
. However, using theLOGURU_LEVEL
environmental variable allows seeing every logging message, for debugging reasons.It may also be worthwhile to temporarily maintain a patched implementation of
grabclipboard()
from Pillow, at least until the next quarterly release of Pillow comes out (on July 1st). One should also be sure to copy the license.The text was updated successfully, but these errors were encountered: