Skip to content

Commit ebd8048

Browse files
authored
Minor refactoring for the tesseract exec logic
Rename command arguments list variable. Proper try/except block with single statement.
1 parent f18f4e5 commit ebd8048

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/pytesseract.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,26 @@ def run_tesseract(input_filename,
148148
lang,
149149
config='',
150150
nice=0):
151-
command = []
151+
cmd_args = []
152152

153153
if not sys.platform.startswith('win32') and nice != 0:
154-
command += ('nice', '-n', str(nice))
154+
cmd_args += ('nice', '-n', str(nice))
155155

156-
command += (tesseract_cmd, input_filename, output_filename_base)
156+
cmd_args += (tesseract_cmd, input_filename, output_filename_base)
157157

158158
if lang is not None:
159-
command += ('-l', lang)
159+
cmd_args += ('-l', lang)
160160

161-
command += shlex.split(config)
161+
cmd_args += shlex.split(config)
162162

163163
if extension != 'box':
164-
command.append(extension)
164+
cmd_args.append(extension)
165+
166+
try:
167+
proc = subprocess.Popen(cmd_args, **subprocess_args())
168+
except OSError:
169+
raise TesseractNotFoundError()
165170

166-
proc = subprocess.Popen(command, **subprocess_args())
167171
status_code, error_string = proc.wait(), proc.stderr.read()
168172
proc.stderr.close()
169173

@@ -177,7 +181,7 @@ def run_and_get_output(image,
177181
extension,
178182
lang=None,
179183
config='',
180-
nice=None,
184+
nice=0,
181185
return_bytes=False):
182186

183187
temp_name, input_filename = '', ''
@@ -191,15 +195,13 @@ def run_and_get_output(image,
191195
'config': config,
192196
'nice': nice
193197
}
194-
try:
195-
run_tesseract(**kwargs)
196-
filename = kwargs['output_filename_base'] + os.extsep + extension
197-
with open(filename, 'rb') as output_file:
198-
if return_bytes:
199-
return output_file.read()
200-
return output_file.read().decode('utf-8').strip()
201-
except OSError:
202-
raise TesseractNotFoundError()
198+
199+
run_tesseract(**kwargs)
200+
filename = kwargs['output_filename_base'] + os.extsep + extension
201+
with open(filename, 'rb') as output_file:
202+
if return_bytes:
203+
return output_file.read()
204+
return output_file.read().decode('utf-8').strip()
203205
finally:
204206
cleanup(temp_name)
205207

0 commit comments

Comments
 (0)