@@ -27,30 +27,33 @@ def process_and_write_results(mocr, img_or_path, write_to):
2727 text = mocr (img_or_path )
2828 t1 = time .time ()
2929
30- logger .info (f' Text recognized in { t1 - t0 :0.03f} s: { text } ' )
30+ logger .info (f" Text recognized in { t1 - t0 :0.03f} s: { text } " )
3131
32- if write_to == ' clipboard' :
32+ if write_to == " clipboard" :
3333 pyperclip .copy (text )
3434 else :
3535 write_to = Path (write_to )
36- if write_to .suffix != '.txt' :
37- raise ValueError ('write_to must be either "clipboard" or a path to a text file' )
36+ if write_to .suffix != ".txt" :
37+ raise ValueError (
38+ 'write_to must be either "clipboard" or a path to a text file'
39+ )
3840
39- with write_to .open ('a' , encoding = "utf-8" ) as f :
40- f .write (text + ' \n ' )
41+ with write_to .open ("a" , encoding = "utf-8" ) as f :
42+ f .write (text + " \n " )
4143
4244
4345def get_path_key (path ):
4446 return path , path .lstat ().st_mtime
4547
4648
47- def run (read_from = 'clipboard' ,
48- write_to = 'clipboard' ,
49- pretrained_model_name_or_path = 'kha-white/manga-ocr-base' ,
50- force_cpu = False ,
51- delay_secs = 0.1 ,
52- verbose = False
53- ):
49+ def run (
50+ read_from = "clipboard" ,
51+ write_to = "clipboard" ,
52+ pretrained_model_name_or_path = "kha-white/manga-ocr-base" ,
53+ force_cpu = False ,
54+ delay_secs = 0.1 ,
55+ verbose = False ,
56+ ):
5457 """
5558 Run OCR in the background, waiting for new images to appear either in system clipboard, or a directory.
5659 Recognized texts can be either saved to system clipboard, or appended to a text file.
@@ -65,21 +68,25 @@ def run(read_from='clipboard',
6568
6669 mocr = MangaOcr (pretrained_model_name_or_path , force_cpu )
6770
68- if sys .platform not in (' darwin' , ' win32' ) and write_to == ' clipboard' :
71+ if sys .platform not in (" darwin" , " win32" ) and write_to == " clipboard" :
6972 # Check if the system is using Wayland
7073 import os
71- if os .environ .get ('WAYLAND_DISPLAY' ):
74+
75+ if os .environ .get ("WAYLAND_DISPLAY" ):
7276 # Check if the wl-clipboard package is installed
7377 if os .system ("which wl-copy > /dev/null" ) == 0 :
7478 pyperclip .set_clipboard ("wl-clipboard" )
7579 else :
76- msg = 'Your session uses wayland and does not have wl-clipboard installed. ' \
77- 'Install wl-clipboard for write in clipboard to work.'
80+ msg = (
81+ "Your session uses wayland and does not have wl-clipboard installed. "
82+ "Install wl-clipboard for write in clipboard to work."
83+ )
7884 raise NotImplementedError (msg )
7985
80- if read_from == ' clipboard' :
86+ if read_from == " clipboard" :
8187 from PIL import ImageGrab
82- logger .info ('Reading from clipboard' )
88+
89+ logger .info ("Reading from clipboard" )
8390
8491 img = None
8592 while True :
@@ -95,19 +102,25 @@ def run(read_from='clipboard',
95102 # Pillow error when clipboard contains text (Linux, X11)
96103 pass
97104 else :
98- logger .warning ('Error while reading from clipboard ({})' .format (error ))
105+ logger .warning (
106+ "Error while reading from clipboard ({})" .format (error )
107+ )
99108 else :
100- if isinstance (img , Image .Image ) and not are_images_identical (img , old_img ):
109+ if isinstance (img , Image .Image ) and not are_images_identical (
110+ img , old_img
111+ ):
101112 process_and_write_results (mocr , img , write_to )
102113
103114 time .sleep (delay_secs )
104115
105116 else :
106117 read_from = Path (read_from )
107118 if not read_from .is_dir ():
108- raise ValueError ('read_from must be either "clipboard" or a path to a directory' )
119+ raise ValueError (
120+ 'read_from must be either "clipboard" or a path to a directory'
121+ )
109122
110- logger .info (f' Reading from directory { read_from } ' )
123+ logger .info (f" Reading from directory { read_from } " )
111124
112125 old_paths = set ()
113126 for path in read_from .iterdir ():
@@ -123,12 +136,12 @@ def run(read_from='clipboard',
123136 img = Image .open (path )
124137 img .load ()
125138 except (UnidentifiedImageError , OSError ) as e :
126- logger .warning (f' Error while reading file { path } : { e } ' )
139+ logger .warning (f" Error while reading file { path } : { e } " )
127140 else :
128141 process_and_write_results (mocr , img , write_to )
129142
130143 time .sleep (delay_secs )
131144
132145
133- if __name__ == ' __main__' :
146+ if __name__ == " __main__" :
134147 fire .Fire (run )
0 commit comments