Spaces:
Runtime error
Runtime error
File size: 4,548 Bytes
5690fd1 c4da61c 5690fd1 6bfb24f 5690fd1 b171503 6bfb24f 5690fd1 6441de2 ccee20f 11026e1 ccee20f 2924302 ccee20f 2924302 38e60f7 de3cb9e 5690fd1 6441de2 5690fd1 6441de2 1991892 6441de2 1991892 5690fd1 1991892 5690fd1 ab4e42c 1991892 3921b70 1991892 b171503 1991892 ab4e42c 5b01a9c 5690fd1 2924302 bc326fa 5690fd1 3921b70 95e09da 3921b70 95e09da f8f326e b171503 5690fd1 619885f c06d6ba 619885f b171503 619885f 5690fd1 9062061 5690fd1 9062061 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 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 = """
<div style="text-align: center;">
<img src="file/logo.png" alt="Logo" width="100"><br>
<h1>HKC Report Generator</h1>
</div>
"""
# Create the Gradio interface with job inputs and uploader name
iface = gr.Interface(
fn=process_images,
inputs=[
gr.File(file_count="multiple", type="filepath", label="Upload Images"), # Allows multiple image uploads
gr.Textbox(label="Job Number"),
gr.Textbox(label="Job Name"),
gr.Textbox(label="Job Address"),
gr.Textbox(label="Uploader Name") # New input for uploader's name
],
outputs=gr.File(label="Download Report"),
title=title_with_logo,
description="Upload images and provide job details to generate a Word report with job information and 2x2 image layout.",
)
# Launch the app with shareable link option
iface.launch(share=True)
|