aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
blob: c9f3c65869027fa9b193e2ec7237d98643bca1f3 (plain) (blame)
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
"""
Created by Biswakalyan Bhuyan
Registration number:18BCB0038

This is a python3 script reads the data of the members from a csv file and images saved as name of the respective members.
Output is saved in output/ with png at output/png/ and pdf at output/pdf/

-->I have included placeholder profile pictures (of lego faces, obviously) at dataset/images/ as the name of the respective members<--

Python Modules required:
1.img2pdf
2.PIL (Pillow)
#import more files
"""

import csv
import os
import img2pdf
from PIL import Image, ImageOps, ImageFont, ImageDraw

csvFile=open('dataset/data.csv', 'r')
reader = csv.reader(csvFile)
for row in reader:

    name,pos,q=row

    f_name,l_name=name.split()

    #splits quote with line breaks
    quote='"'+(q if len(q)<46 else q[:47]+'\n-'+q[47:])+'"'

    #opening image
    img = Image.open("plain.png")
    draw = ImageDraw.Draw(img)

    #opening font files
    font_name = ImageFont.truetype("fonts/Lato-Black.ttf", 130)
    font_pos = ImageFont.truetype("fonts/Lato-Regular.ttf", 64)
    font_quote = ImageFont.truetype("fonts/Lato-Light.ttf", 44)

    #Writing text at specific positions and specific color.
    draw.text((74, 1060),f_name,(0,148,255),font=font_name)
    draw.text((74, 1170),l_name,(0,0,0),font=font_name)
    draw.text((71, 1310),pos,(0,0,0),font=font_pos)
    draw.text((71, 1500),quote,(0,0,0),font=font_quote)

    #A transparent circular image of required size is created for ID-card picture
    size = (770, 770)
    mask = Image.new('L', size, 0)
    dp = ImageDraw.Draw(mask)
    dp.ellipse((0, 0) + size, fill=255)
    im = Image.open('dataset/images/'+name+'.jpg')
    output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
    output.putalpha(mask)

    #Profile image is superimposed on the ID-card image with itself as a filter
    img.paste(output,(180,230),output)

    #Image is resized (with respect to 92dpi) and saved to output/png/
    img = img.resize((322,482), Image.ANTIALIAS)
    img.save('output/png/'+name+'.png')

    #To convert to pdf the image is temporarily stored as jpeg
    rgb_img=img.convert('RGB')
    rgb_img.save("temp.jpg")
    temp=Image.open("temp.jpg")

    #The jpeg file is converted to pdf.
    pdf_bytes = img2pdf.convert("temp.jpg")
    file = open("output/pdf/"+name+".pdf", "wb")
    file.write(pdf_bytes)

os.remove("temp.jpg")
csvFile.close()