Created
February 23, 2016 19:52
-
-
Save allisontharp/064703faa1a7c105860f 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
import glob | |
from PIL import Image, ImageDraw, ImageFont, PdfImagePlugin, _imaging | |
from math import ceil | |
from pyPdf import PdfFileReader,PdfFileWriter | |
import os | |
from datetime import datetime | |
import tkFileDialog | |
startTime = datetime.now() | |
dirname = tkFileDialog.askdirectory() | |
dirname = dirname + "/*.jpg" | |
filenames = glob.glob(dirname) | |
NuImages = len(filenames)-1 | |
NuPages = int(ceil(float(NuImages)/4)) | |
if os.path.isfile('temp2.pdf'): | |
os.remove('temp2.pdf') | |
merged = PdfFileWriter() | |
size = 500,500 | |
width = 500 | |
height = 500 | |
count = 0; | |
for pgnum in range(0,NuPages): | |
print ("Page " + str(pgnum)) | |
w = [] | |
h = [] | |
for num in range(0,4): | |
if num+count < NuImages: | |
im = Image.open(filenames[num+count]) | |
im.thumbnail(size,Image.ANTIALIAS) | |
width,height = im.size | |
w.append(width) | |
h.append(height) | |
width = max(w) | |
height = max(h) | |
blank_image = Image.new("RGB",(width*2+10, height*2+10),"White") | |
draw = ImageDraw.Draw(blank_image) | |
font = ImageFont.truetype('arial.ttf',10) | |
if count <= NuImages: | |
img = Image.open(filenames[count]) | |
img.thumbnail(size,Image.ANTIALIAS) | |
blank_image.paste(img,(0,0)) | |
head, tail = os.path.split(filenames[count]) | |
draw.text((20,20), tail,(200,200,200),font=font) | |
if count+1 <= NuImages: | |
img = Image.open(filenames[count+1]) | |
img.thumbnail(size,Image.ANTIALIAS) | |
blank_image.paste(img,(width+10,0)) | |
head, tail = os.path.split(filenames[count+1]) | |
draw.text((width+30,20), tail,(200,200,200),font=font) | |
if count+2 <= NuImages: | |
img = Image.open(filenames[count+2]) | |
img.thumbnail(size,Image.ANTIALIAS) | |
blank_image.paste(img,(0,height+10)) | |
head, tail = os.path.split(filenames[count+2]) | |
draw.text((20,height+20), tail,(200,200,200),font=font) | |
if count+3 <= NuImages: | |
img = Image.open(filenames[count+3]) | |
img.thumbnail(size,Image.ANTIALIAS) | |
blank_image.paste(img,(width+10,height+10)) | |
head, tail = os.path.split(filenames[count+3]) | |
draw.text((width+30,height+20), tail,(200,200,200),font=font) | |
draw.text((2*width-10,2*height-10), str(pgnum+1),(200,200,200),font=font) | |
blank_image.save("temp2.pdf","pdf") | |
pdf1 = open("temp2.pdf","rb") | |
pdf = PdfFileReader(pdf1) | |
for page in pdf.pages: | |
merged.addPage(page) | |
count = count+4 | |
outputStream = file("document-out.pdf","wb") | |
merged.write(outputStream) | |
pdf1.close() | |
outputStream.close() | |
print("Total Time " + str(datetime.now()-startTime)) | |
print("Time per Image " + str(((datetime.now()-startTime))/NuImages)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment