mirror of
https://github.com/ciromattia/kcc
synced 2025-12-24 07:01:51 +00:00
min crop with page numbers (#1050)
* min crop with page numbers * fix order
This commit is contained in:
@@ -52,6 +52,7 @@ def get_bbox_crop_margin_page_number(img, power=1, background_color='white'):
|
|||||||
'''
|
'''
|
||||||
threshold = threshold_from_power(power)
|
threshold = threshold_from_power(power)
|
||||||
bw_img = img.point(lambda p: 255 if p <= threshold else 0)
|
bw_img = img.point(lambda p: 255 if p <= threshold else 0)
|
||||||
|
ignore_pixels_near_edge(bw_img)
|
||||||
bw_bbox = bw_img.getbbox()
|
bw_bbox = bw_img.getbbox()
|
||||||
if not bw_bbox: # bbox cannot be found in case that the entire resulted image is black.
|
if not bw_bbox: # bbox cannot be found in case that the entire resulted image is black.
|
||||||
return None
|
return None
|
||||||
@@ -142,7 +143,11 @@ def get_bbox_crop_margin(img, power=1, background_color='white'):
|
|||||||
threshold = threshold_from_power(power)
|
threshold = threshold_from_power(power)
|
||||||
bw_img = img.point(lambda p: 255 if p <= threshold else 0)
|
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
|
w, h = bw_img.size
|
||||||
edge_bbox = [
|
edge_bbox = [
|
||||||
(0, 0, w, int(0.02 * h)),
|
(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:
|
if imperfections > 0 and imperfections < .02:
|
||||||
bw_img.paste(im=0, box=box)
|
bw_img.paste(im=0, box=box)
|
||||||
|
|
||||||
return bw_img.getbbox()
|
|
||||||
|
|
||||||
|
|
||||||
def box_intersect(box1, box2, max_dist):
|
def box_intersect(box1, box2, max_dist):
|
||||||
return not (box2[0]-max_dist[0] > box1[1]
|
return not (box2[0]-max_dist[0] > box1[1]
|
||||||
|
|||||||
Reference in New Issue
Block a user