* Copyright (c) 2004-2005 Platon SDG, http://platon.sk/ * Licensed under terms of GNU General Public License. * All rights reserved. * * Changelog: * 2004-11-20 - created * 2005-04-02 - released as an open source * - imported to Platon SDG CVS repositery * */ /* $Platon$ */ if (count($_SERVER['argv']) <= 2) { printf("Usage:\n %s \n", $_SERVER['argv'][0]); exit; } $input_dir = $_SERVER['argv'][1]; $output_dir = $_SERVER['argv'][2]; if (! is_dir($input_dir)) { trigger_error('not a directory: '.$input_dir); exit; } if (! is_dir($output_dir)) { trigger_error('not a directory: '.$output_dir); exit; } // Fetching directories and files data $dir_obj = dir($input_dir); $files = null; while (1) { $entry = $dir_obj->read(); if ($entry === FALSE) { break; } if ($entry == '.' || $entry == '..') { continue; } if (@is_dir($entry)) { } else { $files[] = $entry; } } $dir_obj->close(); foreach ($files as $filename) { echo "Processing $filename -- "; include_once 'Image/OCR.php'; $img = new Image_OCR; if ($img->load("$input_dir/$filename") == false) { trigger_error('image file not found: '.$filename); exit; } $img->setBlackWhite(); $img->invert(); $img->trimAll(); $img->removeLinesIntelligent(); $img->trimAll(); $img->fillAllIntelligent(); $sub_img_ar = $img->splitHorizontalEmptyCols(11 /* min_size */); //printf("Components: %d (%s)\n", count($sub_img_ar), $filename); if (count($sub_img_ar) < 1) { trigger_error('no components in '.$filename); exit; } else if (count($sub_img_ar) < 4) { // split the largest component $max_id = -1; $max_width = -1; for ($i = 0; $i < count($sub_img_ar); $i++) { if ($sub_img_ar[$i]->getWidth() > $max_width) { $max_id = $i; $max_width = $sub_img_ar[$i]->getWidth(); } } $sub_img_ar = array_merge( array_slice($sub_img_ar, 0, $max_id), $sub_img_ar[$max_id]->splitHorizontal(2 /* two components */), array_slice($sub_img_ar, $max_id + 1) ); if (0) { echo "max_id = $max_id, max_width = $max_width\n"; printf("Components: %d (%s)\n", count($sub_img_ar), $filename); } } echo 'writting letter files:'; $filename = basename($filename); $i = 0; foreach ($sub_img_ar as $sub_img) { $letter = $filename[$i]; echo " $letter"; //printf("[%02d]\n", $i + 1); for ($j = 10; file_exists("$output_dir/$letter-$j.txt"); $j++); $sub_img->trimAll(); $sub_img->writeAsciiToFile("$output_dir/$letter-$j.txt"); $i++; } echo "\n"; } /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=0: * }}} */ ?>