import gradio as gr from docx import Document from docx.shared import Inches, Pt, RGBColor from PIL import Image from datetime import datetime import os def get_image_creation_date(img_path): try: img = Image.open(img_path) exif_data = img._getexif() if exif_data and 36867 in exif_data: taken_date = exif_data[36867] return datetime.strptime(taken_date, "%Y:%m:%d %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") except Exception as e: print(f"Error retrieving EXIF data: {e}") creation_date = datetime.fromtimestamp(os.path.getctime(img_path)) return creation_date.strftime("%Y-%m-%d %H:%M:%S") def process_images(images, job_number, job_name, job_address, uploader_name): logo_path = "logo.png" # Replace with the exact name of your uploaded logo doc = Document() # Set narrow margins (0.5 inches or 1.27 cm on all sides) section = doc.sections[0] section.left_margin = Inches(0.5) section.right_margin = Inches(0.5) section.top_margin = Inches(0.5) section.bottom_margin = Inches(0.5) # Add the logo to the header header = section.header.paragraphs[0] run_logo = header.add_run() run_logo.add_picture(logo_path, width=Inches(1.5)) # Logo in the header # Add job details at the beginning of the document body job_details = f"Job: {job_number} - {job_name}, {job_address}" job_paragraph = doc.add_paragraph() run_job = job_paragraph.add_run(job_details) run_job.font.name = "Arial" run_job.font.size = Pt(14) run_job.bold = True run_job.font.color.rgb = RGBColor(0, 0, 0) # Set font color to black uploaded_date = datetime.now().strftime("%Y-%m-%d") # Define image and metadata height requirements image_height = 2.5 # Image height in inches metadata_height = 1 # Metadata height in inches # Define available page height for images (after margins and job details) available_page_height = 10 # Rough page height available in inches (this can vary) # Calculate the number of rows that can fit on one page rows_per_page = available_page_height // (image_height + metadata_height) images_per_page = int(rows_per_page * 2) # Assuming 2 columns per row # Insert images dynamically based on calculated layout for i in range(0, len(images), images_per_page): table = doc.add_table(rows=int(rows_per_page), cols=2) table.autofit = True for j, img_file in enumerate(images[i:i+images_per_page]): row = j // 2 col = j % 2 taken_date = get_image_creation_date(img_file) cell = table.cell(row, col) run = cell.paragraphs[0].add_run() run.add_picture(img_file, width=Inches(2.5)) # Resize to fit # Add metadata below the image with font size 9 and line spacing 1 for text in [ "Description: Describe it here!", f"Taken Date: {taken_date}", f"Uploaded By: {uploader_name}", # Use uploader's name from input f"Uploaded Date: {uploaded_date}", f"File Name: {os.path.basename(img_file)}" ]: paragraph = cell.add_paragraph(text) paragraph.style.font.name = "Arial" paragraph.style.font.size = Pt(9) paragraph.paragraph_format.line_spacing = Pt(9) # Line spacing set to 1 # Save the document output_path = "/tmp/HKC_report_output.docx" doc.save(output_path) return output_path # HTML code to display the logo and title together at the top of the Gradio interface title_with_logo = """
