Skip to content

Instantly share code, notes, and snippets.

@ringerc
Last active December 10, 2015 04:13
Show Gist options
  • Save ringerc/b2d3380c053e9620391c to your computer and use it in GitHub Desktop.
Save ringerc/b2d3380c053e9620391c to your computer and use it in GitHub Desktop.
#!python
# coding=utf-8
#
# usage: python jpegify.py *.pdf
#
# Written for Python 2 or python 3
from __future__ import print_function
import os
import sys
import subprocess
import glob
if 'GSPATH' in os.environ:
gspath = os.environ['GSPATH']
else:
gspath = os.path.join(os.environ['PROGRAMFILES'], r'gs\gs9.18\bin\gswin4')
print("gs is at {}".format(gspath))
if __name__ == '__main__':
print("Time to convert some PDFs")
if len(sys.argv) != 2:
print("Usage: {} *.pdf".format(sys.argv[0]))
sys.exit(1)
if not os.path.exists(gspath):
print("Ghostscript not found at {}, set GSPATH in environment".format(gspath))
sys.exit(1)
print("Examining {} paths for PDFs".format(len(sys.argv[1:])))
pdfs = []
for arg in sys.argv[1:]:
newpdfs = glob.glob(arg)
print("Found {} new PDFs at path {}".format(len(newpdfs), arg))
pdfs.extend(newpdfs)
for pdf in pdfs:
try:
print("Processing PDF {}".format(pdf))
subprocess.check_call([
gspath, '-dNOPAUSE', '-dBATCH', '-sDEVICE=jpeg', '-r300',
'-dJPEGQ=90', '-o', r'{}-%00d.jpg'.format(pdf),
pdf
])
except subprocess.CalledProcessError as ex:
print("Failed to process file {}: {}, skipping".format(pdf, ex))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment