I'm attempting to use ReportLab to create a 4 x 5 grid of images (per page) from a directory of images, in a similar manner to photographic contact sheets. I need to maintain the aspect ratio of the images and also need the filename of each image underneath.
I originally started by using drawimage and adding everything manually but now I think a table and adding the image and filename into each cell might be a better approach. Can anyone give me some pointers on the best way to do this?
I've spent a few days trying to figure this out and I think I am going around in circles with this. Thank you in advance!
Script as of now;
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib.pagesizes import A4
from PIL import Image
import os
def rowGen(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def makePDF(document_title, data):
pdf = SimpleDocTemplate(
document_title,
pagesize = A4
)
style = TableStyle([
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
('BOTTOMPADDING', (0, 0), (-1, -1), 12),
])
table = Table(data) #Creates and adds the data to the table
table.setStyle(style) #Sets style to the above defined style
elements = [] #Creates an empty elements list
elements.append(table) #Lays out the elements
pdf.build(elements)
path = 'Images/'
image_list = [i for i in os.listdir(path) if i.endswith('.jpg')] #list of images in selected directory
data = [i for i in rowGen(image_list, 4)]
makePDF('Test.pdf', data)
So, after a few days of working through some ideas I came up with the below. I am fairly new to this and I'm sure there are better approaches but if it helps anybody: