LetterCropper.py 890 Bytes
Newer Older
1
from copy import deepcopy
2
from Rectangle import Rectangle
3
from GrayscaleImage import GrayscaleImage
4

5
class LetterCropper:
6

Richard Torenvliet's avatar
Richard Torenvliet committed
7
    def __init__(self, threshold = 0.9):
8
        self.threshold = threshold
9
        
10 11
    def crop_to_letter(self, image):
        self.image = image
12
        self.determine_letter_bounds()
13
        self.image.crop(self.letter_bounds)
14 15

    def determine_letter_bounds(self):
16
        min_x = self.image.width
17
        max_x = 0
18
        min_y = self.image.height
19 20
        max_y = 0

21
        for y, x, value in self.image:
22 23 24 25 26
            if value < self.threshold:
                if x < min_x: min_x = x
                if y < min_y: min_y = y
                if x > max_x: max_x = x
                if y > max_y: max_y = y
27
        
28 29 30 31 32
        self.letter_bounds = Rectangle(
            min_x, 
            min_y, 
            max_x - min_x ,
            max_y - min_y
Richard Torenvliet's avatar
Richard Torenvliet committed
33
        )