|
from PIL import Image, ImageDraw, ImageFont |
|
import os |
|
|
|
accepted_extensions = (".jpg", ".jpeg", ".png") |
|
|
|
def create_contact_sheet(image_paths, output_dir, columns=4, rows=6, padding=5): |
|
|
|
image_size = 100 |
|
contact_sheet_width = columns * image_size |
|
contact_sheet_height = rows * image_size |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
current_sheet = 1 |
|
x = 0 |
|
y = 0 |
|
contact_sheet = Image.new('RGB', (contact_sheet_width, contact_sheet_height), color='white') |
|
draw = ImageDraw.Draw(contact_sheet) |
|
|
|
for filename in image_paths: |
|
if filename.lower().endswith(accepted_extensions): |
|
img = Image.open(filename) |
|
|
|
img.thumbnail((image_size - padding * 2, image_size - padding * 2), Image.Resampling.LANCZOS) |
|
|
|
contact_sheet.paste(img, (x + padding, y + padding)) |
|
|
|
draw.text((x + padding, y + image_size - padding), os.path.basename(filename), fill='black') |
|
|
|
x += image_size |
|
if x >= contact_sheet_width: |
|
x = 0 |
|
y += image_size |
|
if y >= contact_sheet_height: |
|
|
|
output_file = os.path.join(output_dir, f'contact_sheet_{current_sheet}.jpg') |
|
contact_sheet.save(output_file, "JPEG") |
|
print(f"Saved contact sheet {current_sheet}") |
|
|
|
current_sheet += 1 |
|
y = 0 |
|
contact_sheet = Image.new('RGB', (contact_sheet_width, contact_sheet_height), color='white') |
|
draw = ImageDraw.Draw(contact_sheet) |
|
|
|
|
|
output_file = os.path.join(output_dir, f'contact_sheet_{current_sheet}.jpg') |
|
contact_sheet.save(output_file, "JPEG") |
|
print(f"Saved contact sheet {current_sheet}") |
|
|
|
return current_sheet |