hackathon-data / output /pdf-image-audit /render_examples.py
julianvolodia's picture
Super-squash branch 'main' using huggingface_hub
90264a9
Raw History Blame Contribute Delete
1.27 kB
"""Reviewed example regions, not an automatic region detector."""
from pathlib import Path
import hashlib,json
import pymupdf as fitz
out=Path('output/pdf-image-audit')
cases=[('data/historia-2025-maj-matura-rozszerzona.pdf',4,[65,87,540,357],'2025-task-1-source-1'),
('data/historia-2018-nowa-era-probna-rozszerzona.pdf',13,[50,74,550,440],'2018-task-14-map')]
rows=[]
for path,page,bbox,name in cases:
hashes=[]
for _ in range(2):
with fitz.open(path) as doc:
pixels=doc[page-1].get_pixmap(matrix=fitz.Matrix(3,3),clip=fitz.Rect(bbox),colorspace=fitz.csRGB,alpha=False,annots=True)
png=pixels.tobytes('png')
hashes.append(hashlib.sha256(png).hexdigest())
assert hashes[0]==hashes[1]
(out/(name+'.png')).write_bytes(png)
rows.append({'pdf':path,'pdf_sha256':hashlib.sha256(Path(path).read_bytes()).hexdigest(),
'page':page,'bbox_top_left_points':bbox,'selection':'manually reviewed example',
'dpi':216,'colorspace':'RGB','alpha':False,'annots':True,
'pymupdf':fitz.VersionBind,'png_sha256':hashes[0],'two_fresh_document_renders_identical':True})
(out/'render-check.json').write_text(json.dumps(rows,indent=2))
print(json.dumps(rows,indent=2))