LetterCropper.py 804 B

12345678910111213141516171819202122232425262728293031
  1. from Rectangle import Rectangle
  2. class LetterCropper:
  3. def __init__(self, threshold = 0.9):
  4. self.threshold = threshold
  5. def crop_to_letter(self, image):
  6. self.image = image
  7. self.determine_letter_bounds()
  8. self.image.crop(self.letter_bounds)
  9. def determine_letter_bounds(self):
  10. min_x = self.image.width
  11. max_x = 0
  12. min_y = self.image.height
  13. max_y = 0
  14. for y, x, value in self.image:
  15. if value < self.threshold:
  16. if x < min_x: min_x = x
  17. if y < min_y: min_y = y
  18. if x > max_x: max_x = x
  19. if y > max_y: max_y = y
  20. self.letter_bounds = Rectangle(
  21. min_x,
  22. min_y,
  23. max_x - min_x ,
  24. max_y - min_y
  25. )