Spaces:
Sleeping
Sleeping
| import io | |
| from typing import List, Dict | |
| from PIL import Image, ImageDraw | |
| import pandas as pd | |
| import uuid, os | |
| def crop_to_png_bytes(img_pil: Image.Image, x:int,y:int,w:int,h:int) -> bytes: | |
| crop = img_pil.crop((x,y,x+w,y+h)) | |
| bio = io.BytesIO() | |
| crop.save(bio, format="PNG") | |
| return bio.getvalue() | |
| def overlay_boxes(img_pil: Image.Image, boxes: List[Dict]) -> Image.Image: | |
| out = img_pil.copy() | |
| dr = ImageDraw.Draw(out) | |
| for b in boxes: | |
| x,y,w,h = b["x"],b["y"],b["w"],b["h"] | |
| dr.rectangle([x,y,x+w,y+h], outline=(124,58,237), width=6) | |
| return out | |
| def to_excel_file(cards: List[Dict]) -> str: | |
| rows=[] | |
| for c in cards: | |
| rows.append({ | |
| "box_id": c.get("box_id"), | |
| "company": c.get("company"), | |
| "person_kanji": c.get("person_kanji"), | |
| "person_kana": c.get("person_kana"), | |
| "person_romaji": c.get("person_romaji"), | |
| "title": c.get("title"), | |
| "department": c.get("department"), | |
| "email": ", ".join(c.get("email", [])), | |
| "phone": ", ".join(c.get("phone", [])), | |
| "website": c.get("website"), | |
| "address_jp": c.get("address_jp"), | |
| "notes": c.get("notes"), | |
| "source_name": c.get("source_name"), | |
| }) | |
| df = pd.DataFrame(rows) | |
| path = f"/tmp/contacts-{uuid.uuid4().hex[:8]}.xlsx" | |
| with pd.ExcelWriter(path, engine="openpyxl") as w: | |
| df.to_excel(w, index=False, sheet_name="Contacts") | |
| return path | |