grob-products-updated / scripts /export_crops.py
bep40's picture
Upload scripts/export_crops.py
df2aecb verified
Raw History Blame Contribute Delete
1.07 kB
import requests, io, os
from PIL import Image
from huggingface_hub import HfApi
H = "https://huggingface.co"
api = HfApi(token=os.environ["HF_TOKEN"], endpoint=H)
pdf_r = requests.get(H + "/datasets/bep40/grob-products-updated/resolve/main/catpages/imudex-2026/page_0080.jpg", timeout=120)
img = Image.open(io.BytesIO(pdf_r.content))
iw, ih = img.size
sx = iw / 1133.86; sy = ih / 793.70
boxes = {
"A_topleft_7801700": (57, 73, 228, 235),
"B_midleft_7801100": (57, 302, 228, 440),
"C_botleft_7804100": (57, 532, 244, 645),
"D_topright_7804104": (622, 79, 809, 203),
}
for label, (x0, y0, x1, y1) in boxes.items():
box = (int(x0*sx), int(y0*sy), int(x1*sx), int(y1*sy))
cr = img.crop(box).copy()
buf = io.BytesIO()
cr.convert("RGB").save(buf, "JPEG", quality=92)
info = api.upload_file(path_or_fileobj=buf.getvalue(), path_in_repo=f"debug_crops/p81_{label}.jpg",
repo_id="bep40/grob-products-updated", repo_type="dataset",
commit_message=f"debug crop {label}")
print("uploaded", label, info.commit_url)
print("DONE")