Skip to content

Instantly share code, notes, and snippets.

@hampelm
Created September 12, 2015 17:34
Show Gist options
  • Save hampelm/43179a466882f34c266c to your computer and use it in GitHub Desktop.
Save hampelm/43179a466882f34c266c to your computer and use it in GitHub Desktop.
from fpdf import FPDF
import Image
import os
# http://pyfpdf.readthedocs.org/en/latest/
PAGE_SIZE = (7, 9) # size of the page in inches (height, width)
COLS = 12
ROWS = 7
WIDTH = 0.63
GUTTER = 0.11
Y_GUTTER = 0.34
IMAGE_SIZE = 200, 200
files = os.listdir('.')
# Get only the JPEGs
file_names_to_process = []
for f in files:
extension = f.split('.')[-1]
if extension == 'jpg':
file_names_to_process.append(f)
pdf = FPDF(orientation = 'L', unit = 'in', format=PAGE_SIZE)
pdf.add_page()
row = 0
col = 0
page = 1
for filename in file_names_to_process:
thumbname = filename.split('.')[0] + '-thumb.jpg'
# print thumbname
try:
print 'Starting ' + thumbname
image = Image.open(filename)
image.thumbnail(IMAGE_SIZE, Image.ANTIALIAS)
image.save(thumbname, 'JPEG')
x = GUTTER + (WIDTH * col) + (GUTTER * col)
y = GUTTER + (WIDTH * row) + (Y_GUTTER * row)
pdf.image(name=thumbname, x = x, y = y, w = WIDTH)
col +=1
if col == COLS and row == ROWS:
page += 1
print 'Adding page ', page
col = 0
row = 0
pdf.add_page()
if col == COLS:
col = 0
row += 1
os.remove(thumbname)
except:
print 'Cannot open', filename
pdf.output('pdf.pdf', 'F')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment