Skip to content

Instantly share code, notes, and snippets.

@DominicBM
Created June 29, 2017 13:22
Show Gist options
  • Save DominicBM/4e5e4ca748842d322e6cb118a01cfb23 to your computer and use it in GitHub Desktop.
Save DominicBM/4e5e4ca748842d322e6cb118a01cfb23 to your computer and use it in GitHub Desktop.
# This script requires Wand for Python. Install using the documentation at http://docs.wand-py.org/en/0.4.1/index.html before running.
import sys, os, datetime
from wand.image import Image
list = os.listdir(os.getcwd())
tuples = []
for file in list:
location = os.path.join(os.getcwd(), file)
size = os.path.getsize(location)
tuples.append((file, size))
tuples.sort(key=lambda s: s[1])
for n in tuples:
item = n[0]
if item.endswith(".pdf"):
start = datetime.datetime.now()
print 'Working on "' + item + '" (' + str(datetime.datetime.now()) + ').'
with Image(filename=item, resolution=300) as readpdf:
pages = len(readpdf.sequence)
folder = os.getcwd() + '/' + item[:-4]
if not os.path.exists(folder):
os.makedirs(folder)
jpgfile = folder + '/' + item[:-4] + '.jpg'
readpdf.compression_quality = 100
readpdf.compression = 'no'
readpdf.COLORSPACE_TYPES = 'srgb'
readpdf.save(filename=jpgfile)
os.rename(item, folder + '/' + item)
end = datetime.datetime.now()
print 'Took ' + str(int(round((end - start).total_seconds()))) + ' seconds for "' + item + '" (' + str(pages) + ' page(s)).'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment