Upload pfd2png_thg.py
Browse files- pfd2png_thg.py +33 -0
pfd2png_thg.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tempfile
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import fitz # pymupdf
|
| 5 |
+
from datasets import Dataset, Image
|
| 6 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 7 |
+
|
| 8 |
+
REPO = "divinity-library/thg-01"
|
| 9 |
+
TARGET = "divinity-library/thg-02"
|
| 10 |
+
MAX_PDFS = None # how many PDFs to process
|
| 11 |
+
MAX_PAGES = None # pages per PDF, None for all
|
| 12 |
+
|
| 13 |
+
api = HfApi()
|
| 14 |
+
pdf_files = sorted(
|
| 15 |
+
f for f in api.list_repo_files(REPO, repo_type="dataset")
|
| 16 |
+
if f.lower().endswith(".pdf") and f.startswith("train/")
|
| 17 |
+
)[:MAX_PDFS]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
out_dir = Path(tempfile.mkdtemp())
|
| 21 |
+
rows = []
|
| 22 |
+
for name in pdf_files:
|
| 23 |
+
pdf_path = Path(hf_hub_download(REPO, name, repo_type="dataset"))
|
| 24 |
+
doc = fitz.open(pdf_path)
|
| 25 |
+
for i, page in enumerate(doc):
|
| 26 |
+
if MAX_PAGES and i >= MAX_PAGES:
|
| 27 |
+
break
|
| 28 |
+
img_path = out_dir / f"{Path(name).stem}_{i:04d}.png"
|
| 29 |
+
page.get_pixmap(dpi=200).save(img_path)
|
| 30 |
+
rows.append({"image": str(img_path), "source": name, "page": i + 1})
|
| 31 |
+
|
| 32 |
+
ds = Dataset.from_list(rows).cast_column("image", Image())
|
| 33 |
+
ds.push_to_hub(TARGET, private=True)
|