def __imageHasContrast(self, image): colors = image.getcolors() has_contrast = True for color_count in colors: color = color_count[1] has_contrast = graphics.colorsContrast(color, self.bg_color, self.contrast_tolerance) if has_contrast: break return has_contrast
def get_deskew_angle(self, image_orig): width, height = image_orig.size resize_ratio = 600 / float(width) # Convert image to grayscale and resize it for better # performance image = image_orig.convert('L') image = image.resize((int(round(width * resize_ratio)), int(round(height * resize_ratio)))) width, height = image.size max_r = int(round(math.sqrt(width**2 + height**2))) hough_accumulator = {} for x in range(0, width): for y in range(0, height - 1): if y + 1 > height: break color = image.getpixel((x, y)) color_below = image.getpixel((x, y + 1)) if graphics.colorsContrast(color, self.bg_color, self.contrast_tolerance) and \ graphics.colorsContrast(color, color_below, self.contrast_tolerance): for r, angle in self.__getDistanceAndAngle(x, y): if 0 < r < max_r: vote_value = hough_accumulator.get((r, angle), 0) hough_accumulator[(r, angle)] = vote_value + 1 if not hough_accumulator: return 0 max_voted = hough_accumulator.keys()[0] for r_angle in hough_accumulator: max_voted_value = hough_accumulator.get(max_voted) if hough_accumulator[r_angle] > max_voted_value: max_voted = r_angle return 90 - max_voted[1]
def get_deskew_angle(self, image_orig): width, height = image_orig.size resize_ratio = 600 / float(width) # Convert image to grayscale and resize it for better # performance image = image_orig.convert('L') image = image.resize((int(round(width * resize_ratio)), int(round(height * resize_ratio)))) width, height = image.size max_r = int(round(math.sqrt(width ** 2 + height ** 2))) hough_accumulator = {} for x in range(0, width): for y in range(0, height - 1): if y + 1 > height: break color = image.getpixel((x, y)) color_below = image.getpixel((x, y + 1)) if graphics.colorsContrast(color, self.bg_color, self.contrast_tolerance) and \ graphics.colorsContrast(color, color_below, self.contrast_tolerance): for r, angle in self.__getDistanceAndAngle(x, y): if 0 < r < max_r: vote_value = hough_accumulator.get((r, angle), 0) hough_accumulator[(r, angle)] = vote_value + 1 if not hough_accumulator: return 0 max_voted = hough_accumulator.keys()[0] for r_angle in hough_accumulator: max_voted_value = hough_accumulator.get(max_voted) if hough_accumulator[r_angle] > max_voted_value: max_voted = r_angle return 90 - max_voted[1]
def __windowContrast(self, bgcolor, x, y): image = self.black_n_white_image width, height = image.size image_upper_left_corner_x = x * self.window_size image_upper_left_corner_y = y * self.window_size i, j = 1, 1 while j < self.window_size + 1: if not image_upper_left_corner_y + j < height: break while i < self.window_size + 1: if not image_upper_left_corner_x + i < width: break pixel_point = (image_upper_left_corner_x + i, image_upper_left_corner_y + j) if graphics.colorsContrast(image.getpixel(pixel_point), bgcolor, self.contrast_tolerance): return 1 i += 3 i = 1 j += 3 return 0