21 lines
664 B
Python
21 lines
664 B
Python
import cv2, os, uuid
|
|
from app.config import settings
|
|
|
|
def crop_article(page_image, bbox, out_dir):
|
|
x, y, w, h = [int(v) for v in bbox]
|
|
crop = page_image[y:y+h, x:x+w]
|
|
os.makedirs(out_dir, exist_ok=True)
|
|
path = os.path.join(out_dir, f"article_{uuid.uuid4().hex[:8]}.png")
|
|
cv2.imwrite(path, cv2.cvtColor(crop, cv2.COLOR_RGB2BGR))
|
|
return path
|
|
|
|
def crop_figures(page_image, article, out_dir):
|
|
paths = []
|
|
for b in article["blocks"]:
|
|
if b["type"] == "Figure":
|
|
paths.append(crop_article(page_image, _xywh(b["bbox"]), out_dir))
|
|
return paths
|
|
|
|
def _xywh(box):
|
|
return [box[0], box[1], box[2]-box[0], box[3]-box[1]]
|