-
Notifications
You must be signed in to change notification settings - Fork 0
/
images_report.py
69 lines (59 loc) · 2.27 KB
/
images_report.py
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
###
# Command format : python(3) [scriptname].py [OutputfolderName] [ImageFolderPath]
###
import sys
import os
from PIL import Image
from io import BytesIO
from PyPDF2 import PdfWriter, PdfReader, PdfMerger
from datetime import datetime
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
# importing the command line arguments in the format
args = sys.argv
# Error if more than 3 args are passed
if len(args) > 3:
raise Exception("Arguments should not be more than 3")
# printing tha args for debugging
print("The output folder name is : " + args[1])
print("Image Folder path : " + args[2])
# creating a dir for report
dirPath = os.getcwd()+'/ImageReports/'
if not os.path.exists(dirPath):
# Create the directory
os.mkdir(dirPath)
print(f"Directory '{dirPath}' created successfully.")
else:
print(f"Directory '{dirPath}' already exists.")
# Set the directory path for the images
image_folder = args[2]
# Create a new PDF file for the title page
title = 'Thermal Sensor Output Report'
date = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
title_pdf = BytesIO()
c = canvas.Canvas(title_pdf)
c.setFont('Helvetica-Bold', 32)
c.drawCentredString(4.25*inch, 7*inch, title)
c.setFont('Helvetica', 12)
c.drawCentredString(4.25*inch, 6.5*inch, f'Date: {date}')
c.showPage()
c.save()
# Create a new PDF file to store the images
output_pdf = PdfMerger()
# Add the title page to the output PDF file
output_pdf.append(PdfReader(title_pdf))
# Loop through all the images in the folder
for image_file in os.listdir(image_folder):
# Check if the file is an image file
if image_file.startswith('Inspected') and (image_file.endswith('.png')):
print(image_file)
# Open the image file
with Image.open(os.path.join(image_folder, image_file)) as image:
# Convert the image to PDF format and add it to the output PDF file
with BytesIO() as f:
image.save(f, format='PDF')
pdf_data = f.getvalue()
output_pdf.append(PdfReader(BytesIO(pdf_data)))
# Write the output PDF file to the specified path
with open(dirPath+'thermal_data_report_'+ datetime.now().strftime("%m-%d-%Y_%H-%M-%S") +'.pdf', 'wb') as f:
output_pdf.write(f)