Skip to content

Commit

Permalink
Pillow Files
Browse files Browse the repository at this point in the history
These are files created for creating marksheet
  • Loading branch information
Ankit-Upadhyay-73 authored Apr 9, 2021
1 parent 0599e73 commit 03b660b
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
135 changes: 135 additions & 0 deletions Marksheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from PIL import Image,ImageFont
from PIL import ImageDraw
import json



# conversion to JSON done by dumps() function

marksheet_data = json.dumps({
"id":1,
"name":"Ankit",
"email":"ankit@indigitalit.com",
"gender":"Male",
"finalresult":"Pass / Fail",
"CGPA":"A",
"message":"Can we do better",
"college":[
{
"id":1,
"name":"Vidyavardhini's Annasaheb Vartak College",
"address":"Vasai Road (W), Dist. Palghar - 401202. Maharashtra, India",
"grade":"A",
"phone":250-2332017,
"email":"avct23_principal@rediffmail.com",
"logo_src":"/Users/surajlad/PHPPractice/PHPResources/vartak_logo.thumbnail",
"reportCardType":"Template 1",
"course":[
{
"id":1,
"name":"BSC IT",
"duration":"6 Months",
"subject":[
{
"id":1,
"name":"Java",
"passingMarks":40,
"outof":100,
"marks":65,
"grade":"A"
},
{
"id":2,
"name":"C++",
"passingMarks":40,
"outof":100,
"marks":75,
"grade":"A"
},
{
"id":3,
"name":"Phython",
"passingMarks":40,
"outof":100,
"marks":85,
"grade":"A"
}
]
}
]
}
]
})

marksheet_data = json.loads(marksheet_data)

fntheader = ImageFont.truetype("/Users/Surajlad/PHPPractice/PHPResources/SourceSerifPro-SemiBold.ttf",25)

fntbold = ImageFont.truetype("/Users/Surajlad/PHPPractice/PHPResources/SourceSerifPro-SemiBold.ttf",18)

fnt = ImageFont.truetype("/Users/Surajlad/PHPPractice/PHPResources/Verdana.ttf", 16)

#marksheet_data = json.loads(input("Marksheeets Input"))


for colleges in marksheet_data["college"]:

#result_layout created
image = Image.new('RGB',(1280,800),'#ffffff')

#draw instance created
image_draw = ImageDraw.Draw(image)

for college_data in colleges:
if college_data == "name":
image_draw.text((260,40),colleges[college_data],font = fntheader,fill="#303931")

if college_data=="logo_src":
image.paste(Image.open(colleges[college_data]),(65,33))

if college_data =="grade":
image_draw.text((963,75),str("Grade:"+str(colleges[college_data])),font = fntbold,fill="#303931")

if college_data == "address":
image_draw.text((290,75),colleges[college_data],font = fntbold,fill="#303931")

if college_data == "phone":
image_draw.text((320,96),str("Phone:"+str(colleges["phone"])),font = fntbold,fill="#303931")

if college_data == "email" :
image_draw.text((460,96),str("Email:"+str(colleges[college_data])),font = fntbold,fill="#303931")

#for student

image_draw.text((59,177),str("Student Name:"+str(marksheet_data["name"])),font = fntbold,fill="#303931")

image_draw.text((61,225),str("Student ID:"+str(marksheet_data["id"])),font = fntbold,fill="#303931")

image_draw.text((191,225),str("course:"+colleges["course"][0]["name"]),font = fntbold,fill="#303931")

#for subject
#title-headers

#to draw text in rectangle
image_draw.text((92,280),"Subject Id",font = fnt,fill="#303931")
image_draw.text((222,280),"Subject Name",font = fnt,fill="#303931")
image_draw.text((372,280),"Weightage",font = fnt,fill="#303931")
image_draw.text((522,280),"Passing",font = fnt,fill="#303931")
image_draw.text((672,280),"Obtained",font = fnt,fill="#303931")

subject_x =92
subject_y =290



for subjects in colleges["course"][0]["subject"]:
subject_y += 50
subject_x =92
for subject_data in subjects:
if subject_data == "id" or "name" or "outof" or "total":
image_draw.text((subject_x,subject_y),str(subjects[subject_data]),font = fnt,fill="#303931")
subject_x += 150
image_draw.rectangle([61,270,1216,310],outline="#000000")
image_draw.line([(92,600),(1216,600)],fill="#303931")

image.show()
57 changes: 57 additions & 0 deletions MarksheetImage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from PIL import Image,ImageFont,ImageDraw
import json

class MarksheetImage(object):

SourceSerif = ImageFont.truetype("/Users/Surajlad/PHPPractice/PHPResources/SourceSerifPro-SemiBold.ttf",25)
Verdana = ImageFont.truetype("/Users/Surajlad/PHPPractice/PHPResources/Verdana.ttf", 16)

def __new__(cls,*args,**kwargs):
obj = super().__new__(cls)
return obj

def __init__(self,Type="RGB",position=(1280,800),color="#000000"):
self.position = position
self.color = color
self.Type = Type
self.image = Image.new(Type,position,color)

def draw(self,draw_type,position,content=None,structure=(3,3),style={"color":"#ffffff","font-family":Verdana,"text-size":18,"font-weight":None}):
self.draw = ImageDraw.Draw(self.image)

if draw_type=="grid":

x_start = position[0]
y_start = position[1]
x_end = position[2]
y_end = position[3]

computed_y = y_end/structure[0]
computed_x = x_end/structure[1]

for x in range(structure[0]):
self.draw.line(((x_start,computed_y),(x_end,computed_y)),fill="#ff0000")
computed_y += y_end/structure[0]


if draw_type=="line":
self.draw.line(position)

if draw_type=="text":
self.draw.text(position,text = content,font = style.get("font-family"),color = style.get("color"))

if draw_type=="image":
self.image.paste(Image.open(content),position)

return self;

def getMarksheet(self):
return self.image;








96 changes: 96 additions & 0 deletions RefactoredMarksheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

from MarksheetImage import MarksheetImage

#drawing starts

inputted_json = [
{
"type": "text",
"value": "users.name",
"ideal": {
"x": 12,
"y": 12,
"rotation": 0,
"transform": 1.2
},
"style": {
"color": "#000000",
"background": null,
"fontFamily": "",
"fontSize": "",
"fontStyle": "italic|underline",
"fontWeight": 400
}
},
{
"type": "image",
"value": "college.logo",
"ideal": {
"x": 12,
"y": 12,
"rotation": 0,
"transform": 1.2
},
"style": {
"color": null,
"background": null,
"fontFamily": null,
"fontSize": null,
"fontStyle": null,
"fontWeight": null
}
},
{
"type": "rectangle",
"value": "1px",
"ideal": {
"x": 12,
"y": 12,
"rotation": 0,
"transform": 1.2
},
"style": {
"color": "#ff0000",
"background": "#ffffff",
"fontFamily": null,
"fontSize": null,
"fontStyle": null,
"fontWeight": null
}
}
]


json_data = json.loads(json.dumps(inputted_json))




marksheet = MarksheetImage().draw("grid",position = [61,270,1200,300],content = "College Name Here")
image = marksheet.getMarksheet()
image.show()

























Binary file added Success_Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions TemplateSheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sys
sys.path.append(".")
from MarksheetImage import MarksheetImage

marksheet = MarksheetImage()

image = marksheet.draw("text",position = (260,40),content ="College Name is Set",style={"color":"#303931"}).get()
image.show()

0 comments on commit 03b660b

Please sign in to comment.