@@ -46,7 +46,7 @@ class TesseractConfig:
4646
4747 langs (str): The languages to be used by Tesseract for OCR.
4848 custom_config (str): Custom configuration string for Tesseract.
49- ouput_encoding (str): The encoding to be used for the output file.
49+ output_encoding (str): The encoding to be used for the output file.
5050
5151 Methods:
5252 __init__(self, image_path: str, output_path: str):
@@ -60,28 +60,37 @@ class TesseractConfig:
6060 text extraction, and saves the extracted text to an output file. Returns 0 if successful, 1 otherwise.
6161 """
6262
63- def __init__ (self , image_path : str , output_path : str ):
63+ def __init__ (self , image_path : str , output_path : str , langs : str = None ):
6464 """
6565 Initializes the OCR4Linux class with command-line arguments.
6666
6767 Attributes:
6868 image_path (str): The path to the input image file.
6969 output_path (str): The path to the output file where results will be saved.
70+ langs (str): The languages to be used by Tesseract for OCR (optional).
7071 oem_mode (int): The OCR Engine Mode (OEM) for Tesseract.
7172 psm_mode (int): The Page Segmentation Mode (PSM) for Tesseract.
72- langs (str): The languages to be used by Tesseract for OCR.
7373 custom_config (str): Custom configuration string for Tesseract.
74- ouput_encoding (str): The encoding to be used for the output file.
74+ output_encoding (str): The encoding to be used for the output file.
7575 """
7676 self .image_path = image_path
7777 self .output_path = output_path
7878 self .oem_mode = 3 # Default LSTM engine
7979 self .psm_mode = 6 # Uniform block of text
8080 self .available_langs = pytesseract .get_languages ()
81- self .langs = '+' .join (filter (None , self .available_langs )
82- ) if self .available_langs else 'eng'
81+
82+ # Use provided languages or default to all available languages
83+ if langs and langs .strip ():
84+ self .langs = langs
85+ print (f"Using specified languages: { langs } " , file = sys .stderr )
86+ else :
87+ self .langs = '+' .join (filter (None , self .available_langs )
88+ ) if self .available_langs else 'eng'
89+ print (
90+ f"Using all available languages: { self .langs } " , file = sys .stderr )
91+
8392 self .custom_config = f'--oem { self .oem_mode } --psm { self .psm_mode } '
84- self .ouput_encoding = 'utf-8'
93+ self .output_encoding = 'utf-8'
8594
8695 def extract_text_with_lines (self , image : Image ) -> str :
8796 """
@@ -115,7 +124,7 @@ def main(self) -> int:
115124 extracted_text = self .extract_text_with_lines (image )
116125
117126 # Save the extracted text to a file
118- with open (self .output_path , 'w' , encoding = self .ouput_encoding ) as file :
127+ with open (self .output_path , 'w' , encoding = self .output_encoding ) as file :
119128 file .write (extracted_text )
120129
121130 return 0
@@ -149,19 +158,21 @@ def __init__(self):
149158 " and text extraction using Tesseract OCR. The script takes an input\n " + \
150159 " based on the language in the image."
151160 self .useges = [
152- "python OCR4Linux.py <image_path> <output_path>" ,
161+ "python OCR4Linux.py <image_path> <output_path> [--langs <languages>] " ,
153162 "python OCR4Linux.py [-l | --list-langs]" ,
154163 "python OCR4Linux.py [-h | --help]"
155164 ]
156165 self .examples = [
157166 "python OCR4Linux.py screenshot.png output.txt" ,
167+ "python OCR4Linux.py screenshot.png output.txt --langs eng+fra+deu" ,
158168 "python OCR4Linux.py -l" ,
159169 "python OCR4Linux.py -h"
160170 ]
161171 self .arguments = [
162172 "file_path: Path to the python script" ,
163173 "image_path: Path to the image file" ,
164174 "output_path: Path to the output text file" ,
175+ "--langs: Specify languages for OCR (e.g., eng+fra+deu)" ,
165176 "-l, --list-langs: List all available languages for OCR in the system" ,
166177 "-h, --help: Display this help message, then exit"
167178 ]
@@ -199,20 +210,24 @@ def check_arguments(self) -> int:
199210 Checks the command line arguments for validity.
200211
201212 Handles the following options:
202- - Standard usage: <image_path> <output_path>
213+ - Standard usage: <image_path> <output_path> [--langs <languages>]
203214 - Help: -h or --help
204215 - List languages: -l or --list-langs
205216
206217 Returns:
207- bool: True if arguments are valid, False otherwise .
218+ int: 0 if help/list was shown, 1 if error, 2 if valid arguments for processing .
208219 """
209220 if len (sys .argv ) == 2 and sys .argv [1 ] in ['-l' , '--list-langs' ]:
210221 self .list_available_languages ()
211222 return 0
212223 elif len (sys .argv ) == 2 and sys .argv [1 ] in ['-h' , '--help' ]:
213224 self .help ()
214225 return 0
215- elif len (sys .argv ) != self .args_num :
226+ elif len (sys .argv ) < self .args_num or len (sys .argv ) > 5 :
227+ # Valid patterns:
228+ # 3 args: script image_path output_path
229+ # 4 args: script image_path output_path --langs=languages
230+ # 5 args: script image_path output_path --langs languages
216231 self .help ()
217232 return 1
218233 return 2
@@ -252,7 +267,8 @@ def main(self):
252267 This function performs the following steps:
253268 1. Checks if the correct number of arguments is provided.
254269 2. Verifies if the image file exists.
255- 3. Creates an instance of the TesseractConfig class and runs the OCR process.
270+ 3. Parses language arguments if provided.
271+ 4. Creates an instance of the TesseractConfig class and runs the OCR process.
256272
257273 Returns:
258274 int: Returns 1 if there is an error with the arguments or image path, otherwise returns the result of the TesseractConfig main function.
@@ -268,8 +284,15 @@ def main(self):
268284 if not self .check_image_path (sys .argv [1 ]):
269285 return 1
270286
287+ # Parse language arguments
288+ langs = None
289+ if len (sys .argv ) >= 4 and sys .argv [3 ] == '--langs' and len (sys .argv ) == 5 :
290+ langs = sys .argv [4 ]
291+ elif len (sys .argv ) == 4 and sys .argv [3 ].startswith ('--langs=' ):
292+ langs = sys .argv [3 ].split ('=' , 1 )[1 ]
293+
271294 # Create an instance of the TesseractConfig class
272- tesseract = TesseractConfig (sys .argv [1 ], sys .argv [2 ])
295+ tesseract = TesseractConfig (sys .argv [1 ], sys .argv [2 ], langs )
273296 return tesseract .main ()
274297
275298
0 commit comments