Skip to content

Instantly share code, notes, and snippets.

@ovuruska
Created September 27, 2024 10:59
Show Gist options
  • Save ovuruska/5a6d308e5292bba69cc95998ac33fc35 to your computer and use it in GitHub Desktop.
Save ovuruska/5a6d308e5292bba69cc95998ac33fc35 to your computer and use it in GitHub Desktop.
PDF Generation for Image Comparison
import csv
from reportlab.lib.pagesizes import landscape
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.platypus import Table, TableStyle
from reportlab.lib.units import inch
from PIL import Image as PILImage
from io import BytesIO
from pathlib import Path
from tqdm import tqdm
def read_csv(file_path):
with open(file_path, 'r') as f:
return list(csv.DictReader(f))
def pil_to_reportlab(pil_img):
img_buffer = BytesIO()
pil_img.save(img_buffer, format='PNG')
img_buffer.seek(0)
return Image(img_buffer)
def create_pdf_chunk(chunk_data, output_pdf):
page_width, page_height = landscape((16 * inch, 9 * inch))
doc = SimpleDocTemplate(str(output_pdf), pagesize=(page_width, page_height))
styles = getSampleStyleSheet()
title_style = ParagraphStyle(
'TitleStyle',
parent=styles['Heading2'],
fontSize=14,
spaceAfter=12,
alignment=1 # Center alignment
)
caption_style = ParagraphStyle(
'CaptionStyle',
parent=styles['Normal'],
fontSize=10,
alignment=1 # Center alignment
)
elements = []
for q_row, o_row in chunk_data:
try:
assert q_row['prompt'] == o_row['prompt'], "Prompts do not match"
title = f"{q_row['prompt']} (seed = {q_row['seed']})"
elements.append(Paragraph(title, title_style))
q_img = PILImage.open(q_row['image_path'])
o_img = PILImage.open(o_row['image_path'])
max_height = page_height * 0.5
q_img.thumbnail((max_height, max_height))
o_img.thumbnail((max_height, max_height))
data = [
[pil_to_reportlab(q_img), pil_to_reportlab(o_img)],
[Paragraph("Quantized", caption_style), Paragraph("Original", caption_style)]
]
t = Table(data, colWidths=[page_width / 2 - 0.5 * inch, page_width / 2 - 0.5 * inch])
t.setStyle(TableStyle([
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(t)
elements.append(PageBreak())
except Exception as e:
print(f"Error processing images: {str(e)}")
print(f"Quantized image path: {q_row['image_path']}")
print(f"Original image path: {o_row['image_path']}")
try:
doc.build(elements)
print(f"PDF chunk generated successfully: {output_pdf}")
except Exception as e:
print(f"Error building PDF chunk: {str(e)}")
def create_chunked_pdfs(quantized_data, original_data, output_pdf_dir, chunk_size=50):
output_pdf_dir = Path(output_pdf_dir)
output_pdf_dir.mkdir(parents=True, exist_ok=True)
data = list(zip(quantized_data, original_data))
chunks = [data[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
for i, chunk in enumerate(tqdm(chunks, desc="Generating PDF chunks")):
output_pdf = output_pdf_dir / f"chunk_{i+1}.pdf"
create_pdf_chunk(chunk, output_pdf)
# Main execution
quantized_data = read_csv('/home/docker/api_image_generation_log.csv')
original_data = read_csv('/home/docker/quantized_image_generation_log.csv')
output_pdf_dir = Path.home() / 'image_comparison_chunks'
output_pdf_dir.mkdir(parents=True, exist_ok=True)
create_chunked_pdfs(quantized_data, original_data, output_pdf_dir, chunk_size=50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment