Skip to content

Commit abb7056

Browse files
author
hetsonii
committed
Initial Commit. 3 scripts
0 parents  commit abb7056

File tree

8 files changed

+251
-0
lines changed

8 files changed

+251
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
certificates/*

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# CertifyD
2+
3+
- Version 1:
4+
- No GUI
5+
- Calculates the coordinates mathematically
6+
7+
- Version 2:
8+
- GUI
9+
- Opens a image window to get the coordinates
10+
- Opens a window to get the template image and data. No defaults set
11+
12+
- Version 3:
13+
- Filename defaults set. (data/template.png and data/names.csv)
14+
- colourful terminal logging
15+
16+
- Version 4:
17+
- Under development
18+
19+

data/names.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
,name,checked_in_at,ticket_name
2+
,ABCD EFGH,5899662.00,Standard
3+
,IJKL MNOP,5899662.00,Standard
4+
,QRST UVWX,5899662.00,Standard
5+
,YZAB CDEF,5899662.00,Standard
6+

data/template.png

1.66 MB
Loading

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
chardet==5.2.0
2+
pillow==10.2.0
3+
reportlab==4.1.0
4+
setuptools==59.6.0
5+
colorama==0.4.6
6+
pyfiglet==1.0.2
7+
termcolor==2.4.0

script_v1.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import csv
2+
import os
3+
from PyPDF2 import PdfReader, PdfWriter
4+
from reportlab.lib.pagesizes import letter
5+
from reportlab.pdfgen import canvas
6+
from PIL import Image, ImageDraw, ImageFont
7+
8+
def write_names_on_file(input_file, csv_path, output_folder):
9+
name, ext = os.path.splitext(input_file)
10+
if ext.lower() == '.pdf':
11+
write_names_on_pdf(input_file, csv_path, output_folder)
12+
elif ext.lower() in ['.jpg', '.jpeg', '.png', '.gif']:
13+
write_names_on_image(input_file, csv_path, output_folder)
14+
else:
15+
print(f"Unsupported file type: {ext}")
16+
17+
def get_middle_coordinates(width, height, text, font_size):
18+
text_width = font_size * len(text) * 0.6 # Rough approximation of text width
19+
x = (width - text_width) / 2
20+
y = (height - font_size) / 2
21+
return x, y
22+
23+
def write_names_on_pdf(pdf_path, csv_path, output_folder):
24+
with open(csv_path, 'r') as csv_file:
25+
csv_reader = csv.DictReader(csv_file)
26+
for row in csv_reader:
27+
name = row['name']
28+
output_pdf_path = os.path.join(output_folder, f"{name}.pdf")
29+
reader = PdfReader(pdf_path)
30+
writer = PdfWriter()
31+
32+
for page_num in range(len(reader.pages)):
33+
page = reader.pages[page_num]
34+
writer.add_page(page)
35+
36+
width, height = letter
37+
x, y = get_middle_coordinates(width, height, name, 12) # Adjust font size as needed
38+
packet = canvas.Canvas("temp.pdf", pagesize=letter)
39+
# packet.drawString(x-200, y-40, ' '.join(name.split()[:2]), fontName="Helvetica", fontSize=12)
40+
packet.setFont("Helvetica", 12) # Setting font to Helvetica with size 12
41+
packet.drawString(x-200, y-40, ' '.join(name.split()[:2]))
42+
packet.save()
43+
44+
overlay = PdfReader("temp.pdf")
45+
page.merge_page(overlay.pages[0])
46+
47+
with open(output_pdf_path, 'wb') as output_pdf:
48+
writer.write(output_pdf)
49+
50+
os.remove("temp.pdf") # Remove temporary file
51+
52+
def write_names_on_image(image_path, csv_path, output_folder):
53+
with open(csv_path, 'r') as csv_file:
54+
csv_reader = csv.DictReader(csv_file)
55+
for row in csv_reader:
56+
name = row['name']
57+
output_image_path = os.path.join(output_folder, f"{name}{os.path.splitext(image_path)[1]}")
58+
image = Image.open(image_path)
59+
draw = ImageDraw.Draw(image)
60+
width, height = image.size
61+
x, y = get_middle_coordinates(width, height, ' '.join(name.split()[:2]), 40) # Adjust font size as needed
62+
draw.text((x-200, y-40), name.title(), fill="black", font=ImageFont.truetype("arial.ttf", size=30))
63+
image.save(output_image_path)
64+
65+
66+
input_file = "data/template.png"
67+
csv_file = "data/names.csv"
68+
output_folder = "certificates"
69+
70+
if not os.path.exists(output_folder):
71+
os.makedirs(output_folder)
72+
73+
write_names_on_file(input_file, csv_file, output_folder)

script_v2.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import csv
2+
import os
3+
from PIL import Image, ImageDraw, ImageFont
4+
import tkinter as tk
5+
from tkinter import filedialog
6+
7+
# Global variables for coordinates
8+
x, y = None, None
9+
10+
def get_user_coordinates(img):
11+
global x, y
12+
13+
def on_click(event):
14+
global x, y
15+
x, y = event.x, event.y
16+
root.destroy()
17+
18+
root = tk.Tk()
19+
root.title("Select Coordinates")
20+
root.geometry("+100+100")
21+
root.bind("<Button-1>", on_click)
22+
23+
canvas = tk.Canvas(root, width=img.width, height=img.height)
24+
canvas.pack()
25+
photo_image = tk.PhotoImage(file=img.filename)
26+
canvas.create_image(0, 0, anchor=tk.NW, image=photo_image)
27+
28+
root.mainloop()
29+
30+
def write_names_on_image(image_path, csv_path, output_folder):
31+
global x, y
32+
get_user_coordinates(Image.open(image_path)) # Call once to get coordinates
33+
with open(csv_path, 'r') as csv_file:
34+
csv_reader = csv.DictReader(csv_file)
35+
for row in csv_reader:
36+
name = row['name']
37+
output_image_path = os.path.join(output_folder, f"{name}{os.path.splitext(image_path)[1]}")
38+
image = Image.open(image_path)
39+
draw = ImageDraw.Draw(image)
40+
draw.text((x, y), name.title(), fill="black", font=ImageFont.truetype("arial.ttf", size=30))
41+
image.save(output_image_path)
42+
43+
# Example usage:
44+
input_file = filedialog.askopenfilename(title="Select input image file")
45+
csv_file = filedialog.askopenfilename(title="Select CSV file")
46+
output_folder = "certificates"
47+
48+
if not os.path.exists(output_folder):
49+
os.makedirs(output_folder)
50+
51+
write_names_on_image(input_file, csv_file, output_folder)

script_v3.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import csv
2+
import os
3+
from PIL import Image, ImageDraw, ImageFont
4+
import tkinter as tk
5+
from tkinter import filedialog
6+
from colorama import init, Fore
7+
from termcolor import colored
8+
import pyfiglet
9+
10+
# Initialize colorama
11+
init()
12+
13+
# Global variables for coordinates
14+
x, y = None, None
15+
16+
# Icons for logs
17+
ICONS = {
18+
'info': colored('[ ℹ ]', 'blue'),
19+
'success': colored('[ ✔ ]', 'green'),
20+
'warning': colored('[ ⚠ ]', 'yellow'),
21+
'error': colored('[ ✖ ]', 'red'),
22+
}
23+
24+
def get_user_coordinates(img):
25+
global x, y
26+
27+
def on_click(event):
28+
global x, y
29+
x, y = event.x, event.y
30+
root.destroy()
31+
32+
root = tk.Tk()
33+
root.title("Select Coordinates")
34+
root.geometry("+100+100")
35+
root.bind("<Button-1>", on_click)
36+
37+
canvas = tk.Canvas(root, width=img.width, height=img.height)
38+
canvas.pack()
39+
photo_image = tk.PhotoImage(file=img.filename)
40+
canvas.create_image(0, 0, anchor=tk.NW, image=photo_image)
41+
42+
root.mainloop()
43+
44+
def write_names_on_image(image_path, csv_path, output_folder):
45+
global x, y
46+
get_user_coordinates(Image.open(input_file)) # Call once to get coordinates
47+
confirmed = False
48+
49+
while not confirmed:
50+
with open(csv_path, 'r') as csv_file:
51+
csv_reader = csv.DictReader(csv_file)
52+
for row in csv_reader:
53+
name = row['name']
54+
output_image_path = os.path.join(output_folder, f"{name}{os.path.splitext(image_path)[1]}")
55+
image = Image.open(image_path)
56+
draw = ImageDraw.Draw(image)
57+
draw.text((x, y), name.title(), fill="black", font=ImageFont.truetype("arial.ttf", size=30))
58+
image.save(output_image_path)
59+
print(f"{ICONS['success']} {colored(f'Name {name} placed on image {os.path.basename(output_image_path)}', 'green')}")
60+
61+
# Show preview and ask for confirmation
62+
preview_image = Image.open(output_image_path)
63+
preview_image.show()
64+
confirmed = tk.messagebox.askyesno("Confirmation", "Is the location correct?")
65+
66+
if not confirmed:
67+
get_user_coordinates(Image.open(input_file))
68+
69+
print(f"{ICONS['info']} {colored('Task completed successfully!', 'blue')}")
70+
71+
72+
73+
banner = pyfiglet.figlet_format("CertifyD", font="slant")
74+
print(banner)
75+
76+
# Default files
77+
input_file = 'data/template.png'
78+
csv_file = 'data/names.csv'
79+
80+
if not (os.path.exists(input_file) or os.path.exists(csv_file)):
81+
print(f"{ICONS['info']} {colored('Default files template.png or names.csv not found. Please select files.', 'blue')}")
82+
input_file = filedialog.askopenfilename(title="Select input image file")
83+
csv_file = filedialog.askopenfilename(title="Select CSV file")
84+
85+
86+
output_folder = "certificates"
87+
88+
if not os.path.exists(output_folder):
89+
os.makedirs(output_folder)
90+
91+
92+
print(f"{ICONS['info']} {colored('Writing names on images...', 'blue')}")
93+
write_names_on_image(input_file, csv_file, output_folder)
94+
print(f"{ICONS['info']} {colored('Task completed successfully!', 'blue')}")

0 commit comments

Comments
 (0)