import re,subprocess,glob,os from PIL import Image seal=Image.open('seal.png').convert('RGBA') for f in sorted(glob.glob('*.svg')): s=open(f).read() m=re.search(r']*/>',s) x,y,w,h=map(float,m.groups()) open('_tmp.svg','w').write(s.replace(m.group(0),'')) W,H=(732,732) if 'Magnetic' in f else (709,650) vb=list(map(float,re.search(r'viewBox="([^"]+)"',s).group(1).split())) sx,sy=W/vb[2],H/vb[3] out=f[:-4]+'.png' subprocess.run(['rsvg-convert','-w',str(W),'-h',str(H),'-o',out,'_tmp.svg'],check=True) base=Image.open(out).convert('RGBA') sz=(round(w*sx),round(h*sy)); s2=seal.resize(sz,Image.LANCZOS) base.alpha_composite(s2,(round(x*sx),round(y*sy))) base.save(out); print(out,sz,(round(x*sx),round(y*sy))) os.remove('_tmp.svg')