[ocrfeeder] Fix convertPixbufToImage



commit 16c6af15fa6ad999f936df1c0d496f025d374db6
Author: Joaquim Rocha <me joaquimrocha com>
Date:   Wed Jul 16 22:39:47 2014 +0200

    Fix convertPixbufToImage
    
    Using GI, subpixbufs share the same pixels as the original pixbufs
    but point to the starting pixel given by the subpixbuf's x and y.
    This means that, in order to convert the subpixbuf to a Pillow Image,
    the right pixels buffer needs to be extracted from the original one.

 src/ocrfeeder/studio/widgetModeler.py |    6 ++++++
 src/ocrfeeder/util/graphics.py        |   15 +++++++++++++++
 2 files changed, 21 insertions(+), 0 deletions(-)
---
diff --git a/src/ocrfeeder/studio/widgetModeler.py b/src/ocrfeeder/studio/widgetModeler.py
index c14e0dc..5d29bec 100644
--- a/src/ocrfeeder/studio/widgetModeler.py
+++ b/src/ocrfeeder/studio/widgetModeler.py
@@ -174,6 +174,9 @@ class ImageReviewer(Gtk.HPaned):
                                                     data_box.getY(),
                                                     new_pixbuf_width,
                                                     new_pixbuf_height)
+        subpixbuf.x = data_box.getX()
+        subpixbuf.y = data_box.getY()
+        subpixbuf.width = pixbuf_width
         image = graphics.convertPixbufToImage(subpixbuf)
         layout_analysis = LayoutAnalysis(engine,
                                          clean_text = self.configuration_manager.clean_text)
@@ -924,6 +927,9 @@ class Editor:
         sub_pixbuf = self.pixbuf.new_subpixbuf(x, y,
                                                min(width, pixbuf_width),
                                                min(height, pixbuf_height))
+        sub_pixbuf.x = x
+        sub_pixbuf.y = y
+        sub_pixbuf.width = pixbuf_width
         self.data_box.setImage(sub_pixbuf)
 
     def updateOcrEngines(self, engines_list):
diff --git a/src/ocrfeeder/util/graphics.py b/src/ocrfeeder/util/graphics.py
index 08b23c2..fc8fc6f 100644
--- a/src/ocrfeeder/util/graphics.py
+++ b/src/ocrfeeder/util/graphics.py
@@ -59,6 +59,21 @@ def convertPixbufToImage(pixbuf):
     dimensions = pixbuf.get_width(), pixbuf.get_height()
     pixels = pixbuf.get_pixels()
     mode = pixbuf.get_has_alpha() and "RGBA" or "RGB"
+    num_channels = len(mode)
+
+    # When calling get_pixels() on subpixbufs, the buffer is the same
+    # as the original pixbuf's but the first character is given by the
+    # x and y of the subpixbuf. This means that we have to extract the
+    # right buffer part corresponding only to the subpixbuf's pixels when
+    # creating the Image from bytes.
+    if pixbuf.get_byte_length() > num_channels * dimensions[0] * dimensions[1]:
+        i = 0
+        p = ''
+        for j in range(pixbuf.get_height()):
+            p += pixels[i:i + pixbuf.get_width() * num_channels]
+            i += pixbuf.width * num_channels
+        pixels = p
+
     return Image.frombytes(mode, dimensions, pixels)
 
 def rgbaToInteger(rgba):


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