diff --git a/kindlecomicconverter/image.py b/kindlecomicconverter/image.py index d7435af..5f96699 100755 --- a/kindlecomicconverter/image.py +++ b/kindlecomicconverter/image.py @@ -429,6 +429,10 @@ class ComicPage: bbox = get_bbox_crop_margin(self.image, power, self.fill) if bbox: + w, h = self.image.size + left, upper, right, lower = bbox + # don't crop more than 10% of image + bbox = (min(0.1*w, left), min(0.1*h, upper), max(0.9*w, right), max(0.9*h, lower)) self.maybeCrop(bbox, minimum) def cropInterPanelEmptySections(self, direction): diff --git a/kindlecomicconverter/page_number_crop_alg.py b/kindlecomicconverter/page_number_crop_alg.py index 21254f1..43267ec 100644 --- a/kindlecomicconverter/page_number_crop_alg.py +++ b/kindlecomicconverter/page_number_crop_alg.py @@ -141,6 +141,21 @@ def get_bbox_crop_margin(img, power=1, background_color='white'): ''' threshold = threshold_from_power(power) bw_img = img.point(lambda p: 255 if p <= threshold else 0) + + # ignore pixels near the edges + w, h = bw_img.size + edge_bbox = [ + (0, 0, w, int(0.02 * h)), + (0, int(0.98 * h), w, h), + (0, 0, int(0.02 * w), h), + (int(0.98 * w), 0, w, h) + ] + for box in edge_bbox: + edge = bw_img.crop(box) + h = edge.histogram() + imperfections = h[255] / (edge.height * edge.width) + if imperfections > 0 and imperfections < .02: + bw_img.paste(im=0, box=box) return bw_img.getbbox()