[ocrfeeder] Add support for multiple image TIFFs



commit 3c2202b8e0e98d8cadbfc36361b8144094a3477c
Author: Joaquim Rocha <me joaquimrocha com>
Date:   Sun Jul 13 09:55:11 2014 +0200

    Add support for multiple image TIFFs
    
    gb#624044

 bin/ocrfeeder-cli.in                  |    8 +++++-
 src/ocrfeeder/studio/widgetModeler.py |    3 ++
 src/ocrfeeder/util/graphics.py        |   37 ++++++++++++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 3 deletions(-)
---
diff --git a/bin/ocrfeeder-cli.in b/bin/ocrfeeder-cli.in
index bdb8ad2..809dcfa 100644
--- a/bin/ocrfeeder-cli.in
+++ b/bin/ocrfeeder-cli.in
@@ -31,7 +31,7 @@ if os.path.exists(installed_src):
 
 from PIL import Image
 from ocrfeeder.util.constants import OCRFEEDER_STUDIO_VERSION
-from ocrfeeder.util.graphics import getImageResolution
+from ocrfeeder.util.graphics import getImageResolution, convertMultiImagesInList
 from ocrfeeder.util.configuration import ConfigurationManager
 from ocrfeeder.feeder.ocrEngines import OcrEnginesManager
 from ocrfeeder.studio.dataHolder import PageData
@@ -123,7 +123,11 @@ if options.language:
     ocr_engine.setLanguage(options.language)
 
 pages = []
-for image in images:
+
+image_list = convertMultiImagesInList(images,
+                                  configuration_manager.TEMPORARY_FOLDER)
+
+for image in image_list:
     if not os.path.isfile(image):
         parser.error('The image "%s" is not a file or does not exist.' %\
                      image)
diff --git a/src/ocrfeeder/studio/widgetModeler.py b/src/ocrfeeder/studio/widgetModeler.py
index 5200960..924dbf5 100644
--- a/src/ocrfeeder/studio/widgetModeler.py
+++ b/src/ocrfeeder/studio/widgetModeler.py
@@ -310,6 +310,9 @@ class ImageReviewer_Controler:
 
     def addImages(self, image_path_list):
         item_list = []
+        temp_dir = self.configuration_manager.TEMPORARY_FOLDER
+        image_path_list = graphics.convertMultiImagesInList(image_path_list,
+                                                            temp_dir)
         item_list_length = len(image_path_list)
         if not self.configuration_manager.deskew_images_after_addition and \
            not self.configuration_manager.unpaper_images_after_addition:
diff --git a/src/ocrfeeder/util/graphics.py b/src/ocrfeeder/util/graphics.py
index c1a858a..08b23c2 100644
--- a/src/ocrfeeder/util/graphics.py
+++ b/src/ocrfeeder/util/graphics.py
@@ -19,10 +19,12 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ###########################################################################
 
-from lib import debug
+from lib import debug, getNonExistingFileName
 from PIL import Image
 from gi.repository import GdkPixbuf
 import math
+import imghdr
+import os
 
 def getContainerRectangle(points_list):
     points_list = list(points_list)
@@ -164,3 +166,36 @@ def getImageRotated(image, angle):
     collage = Image.new('RGBA', transparent_bg_image.size, 'white')
     collage.paste(transparent_bg_image, (0, 0), transparent_bg_image)
     return collage
+
+def convertMultiImage(image_path, temp_dir):
+    converted_paths = []
+    if imghdr.what(image_path) != 'tiff':
+        return [image_path]
+
+    debug('Checking for multiple images in TIFF')
+    i = 0
+    base_name = os.path.basename(image_path)
+    name, extension = os.path.splitext(base_name)
+    image = Image.open(image_path)
+    try:
+        while True:
+            image.seek(i)
+            file_name = os.path.join(temp_dir, name + ' #' + str(i + 1) + \
+                                     extension)
+            image_name = getNonExistingFileName(file_name)
+            image.save(image_name, format='TIFF')
+            debug('Saved %s' % image_name)
+            converted_paths.append(image_name)
+            i += 1
+    except EOFError:
+        # No more images in the file
+        pass
+
+    return converted_paths
+
+def convertMultiImagesInList(image_list, temp_dir):
+    for i in range(len(image_list)):
+        converted_images = convertMultiImage(image_list[i], temp_dir)
+        converted_list = image_list[:i] + converted_images + image_list[i + 1:]
+        image_path_list = converted_list
+    return converted_list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]