Spaces:
Paused
Paused
File size: 1,776 Bytes
dac3297 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import sys, warnings, json
warnings.filterwarnings('ignore')
from PIL import Image, ImageDraw, ImageFont
Image.MAX_IMAGE_PIXELS = None
import fitz
page_num = int(sys.argv[1])
det_file = sys.argv[2]
out_name = sys.argv[3]
with open(det_file) as f:
hits = json.load(f)['hits']
pdf = fitz.open(r'..\raw_pdfs\355 E 86th St Mechanical 100% CD Set_flattened.pdf')
page = pdf[page_num - 1]
mat = fitz.Matrix(300/72, 300/72)
pix = page.get_pixmap(matrix=mat, alpha=False)
img = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
pdf.close()
draw = ImageDraw.Draw(img)
try: font = ImageFont.truetype('arial.ttf', 50)
except: font = ImageFont.load_default()
colors = {
'AC-A':(255,0,0),'AC-B':(255,100,0),'AC-C':(255,200,0),
'AC-D':(200,0,0),'AC-E':(0,200,0),'AC-F':(0,0,255),
'AC-G':(180,0,180),'AC-H':(0,180,180),'AC-I':(255,0,255),
'AC-J':(128,128,0),'AC-K':(128,0,128),'AC-L':(0,128,128),
'AC-M':(255,140,0),'AC-N':(140,70,0),'AC-O':(255,105,180),
'AC-P':(0,180,0),'AC-Q':(0,180,180),'AC-R':(165,42,42),
'AC-S':(255,140,0),'AC-T':(0,0,255),'AC-U':(180,0,180),
}
for h in hits:
x,y,w,ht,tag = h['x'],h['y'],h['w'],h['h'],h['tag']
color = colors.get(tag,(0,0,0))
draw.rectangle([x, y, x+w, y+ht], outline=color, width=8)
draw.rectangle([x, y-60, x+170, y], fill=color)
draw.text((x+5, y-55), tag, fill=(255,255,255), font=font)
# Crop to main drawing area, scale to manageable size
W, H = img.size
crop = img.crop((int(W*0.05), int(H*0.02), int(W*0.78), int(H*0.95)))
crop = crop.resize((2200, int(2200 * crop.size[1]/crop.size[0])), Image.LANCZOS)
crop.save(out_name)
print(f'Saved {out_name} {crop.size}')
print(f'Counts:', dict((t, sum(1 for h in hits if h['tag']==t)) for t in sorted({h['tag'] for h in hits})))
|