Last active
January 25, 2016 02:25
-
-
Save lexifdev/d418ac35f16a8a3fce6c to your computer and use it in GitHub Desktop.
This file contains 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
from os import path | |
import os | |
from io import BytesIO | |
from wand.image import Image | |
from PyPDF2 import PdfFileWriter, PdfFileReader | |
base_dir = path.dirname(__file__) | |
data_dir = path.join(base_dir, '..', 'data') | |
asset_dir = path.join(base_dir, '..', 'out') | |
filenames = os.listdir(data_dir) | |
for filename in filenames: | |
name, ext = path.splitext(filename) | |
result_dir = path.join(asset_dir, name) | |
if not path.isdir(result_dir): | |
os.mkdir(result_dir) | |
filepath = path.join(data_dir, filename) | |
original_pdf = PdfFileReader(open(filepath, 'rb')) | |
for i in range(original_pdf.numPages): | |
pdf_file = BytesIO() | |
new_pdf = PdfFileWriter() | |
new_pdf.addPage(original_pdf.getPage(i)) | |
new_pdf.write(pdf_file) | |
pdf_file.seek(0) | |
with Image(file=pdf_file, resolution=300) as image: | |
image_path = path.join(result_dir, '{0:03d}.png'.format(i)) | |
image.save(filename=image_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment