Beispiel #1
0
def _text_recognition_dump(result, args=None, kwargs=None, dump_dir=None):
    cbounds = Config.instance().config('argv:cbounds', (0, 0, 255)).value
    image = args[1]
    storage = kwargs['storage']['TextRecognitor']
    text, text_candidates = storage['text'], storage['text_candidates']
    graphical = storage['graphical']

    # Dump text region candidates
    image1 = image.copy()
    for t in text_candidates:
        t.display_bounds(image1, color=cbounds)
    image1.save(os.path.join(dump_dir, 'candidates.png'))

    # Dump recognized text regions
    image2 = image.copy()
    for t in text:
        t.display_bounds(image2, color=cbounds)
    image2.save(os.path.join(dump_dir, 'recognized.png'))

    # Dump difference text candidates and recognized text regions
    ImageChops.difference(image1, image2).save(
        os.path.join(dump_dir, 'difference.png'))

    # Dump remaining graphical segments
    image3 = Image.create(image.mode, image.width, image.height, background=(255, 255, 255))
    for g in graphical:
        g.display(image3, color=g.color)
    image3.save(os.path.join(dump_dir, 'graphical.png'))
Beispiel #2
0
 def toimage(self, mode="L", color=255, background=0, border=0, angle=None):
     """Convert this segment to image.
     
     :param mode: mode of resulting image
     :param color: color of segment pixels on resulting image
     :param border: image border width (segment pixels will be surrounded by
         border of background color if value is greater than 0)
     :param angle: can be used to create rotated image (usefull for OCR to
         recognize vertical text segments by rotating them to be a
         horizontal text segments)"""
     result = Image.create(mode, self.width + 2 * border, self.height + 2 * border, background=background)
     p = result.pixels
     l, t = self.left, self.top
     for x, y in self.area:
         p[x - l + border, y - t + border] = color
     if angle:
         return result.rotate(angle)
     else:
         return result
Beispiel #3
0
    def run(self):
        """Execute application and return exit code."""
        # Load source image
        source = Image.load(self.infile)
        if source.mode != 'RGB':
            source = source.convert('RGB')
        
        # Create filter stack
        f = None
        f = Parser(next_filter=f)
        f = FigureRecognitor(next_filter=f)
        f = TextRecognitor(next_filter=f)
        f = Segmentizer(next_filter=f)
        f = Quantizer(next_filter=f)

        # Execute filter stack
        f(source, storage={}, key=source.checksum()).save(self.outfile)

        return 0
Beispiel #4
0
 def __create_result_image(self, image, clusters):
     """Create result image by changing color of each pixel in source image
     to one of cluster centroid colors."""
     encoder = self.__c_encoder
     decoder = self.__c_decoder
     res = Image.create(image.mode, image.width, image.height)
     dpix = res.pixels
     spix = image.pixels
     cache = {}
     for y in xrange(image.height):
         for x in xrange(image.width):
             s = spix[x, y]
             if s not in cache:
                 for c in clusters:
                     if encoder(s) in c.samples:
                         val = decoder(c.centroid)
                         break
                 cache[s] = val
                 dpix[x, y] = val
             else:
                 dpix[x, y] = cache[s]
     return res