Last active
April 29, 2021 11:17
-
-
Save sixtyfive/fb59dfe354aea4d960d41524eee954df to your computer and use it in GitHub Desktop.
don't cache 1000 operations => don't leak RAM ... also cf. https://github.com/libvips/pyvips/issues/88
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os.path | |
import pyvips | |
infilepath = '/path/to/file.pdf' | |
basename = os.path.basename(infilepath).split('.')[0] | |
infile = open(infilepath, 'rb').read() | |
pyvips.voperation.cache_set_max(10) # 0 = no parallelisation at all | |
pdf = pyvips.Image.pdfload_buffer(infile, n=-1, sequential=True) | |
npages = pdf.get('n-pages') | |
for i in range(1, npages): | |
print('.', end='', flush=True) | |
outfilepath = f"/tmp/{basename}_page{i:03}.png" | |
image = pyvips.Image.pdfload_buffer(infile, page=i, dpi=300, scale=1.0, sequential=True) | |
open(outfilepath, 'wb').write(image.write_to_buffer('.png')) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment