Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Last active April 29, 2021 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixtyfive/fb59dfe354aea4d960d41524eee954df to your computer and use it in GitHub Desktop.
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
#!/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