1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-12 17:26:23 +00:00

min crop with page numbers (#1050)

* min crop with page numbers

* fix order
This commit is contained in:
Alex Xu
2025-07-26 19:13:15 -07:00
committed by GitHub
parent 7f8f3e67b7
commit 741cab68f3

View File

@@ -52,6 +52,7 @@ def get_bbox_crop_margin_page_number(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_edge(bw_img)
bw_bbox = bw_img.getbbox()
if not bw_bbox: # bbox cannot be found in case that the entire resulted image is black.
return None
@@ -142,7 +143,11 @@ 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
ignore_pixels_near_edge(bw_img)
return bw_img.getbbox()
def ignore_pixels_near_edge(bw_img):
w, h = bw_img.size
edge_bbox = [
(0, 0, w, int(0.02 * h)),
@@ -157,8 +162,6 @@ def get_bbox_crop_margin(img, power=1, background_color='white'):
if imperfections > 0 and imperfections < .02:
bw_img.paste(im=0, box=box)
return bw_img.getbbox()
def box_intersect(box1, box2, max_dist):
return not (box2[0]-max_dist[0] > box1[1]