Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 155 lines (134 sloc) 5.852 kb
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
1 #
2 # Copyright (c) 2007 Silver Stripe Software Pvt Ltd
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 # THE SOFTWARE.
21 #
22 #
23 # Contributions and bug reports are welcome. Send an email to
24 # siddharta at silverstripesoftware dot com
25 #
26
706ed9c Fixed import error and indentation error
Silver Stripe Software authored
27 from PIL import Image, ImageDraw, ImageFont
1a7d40d @kausikram made colours configurable
kausikram authored
28 import os
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
29 from csvimport import CsvReader
30
1a7d40d @kausikram made colours configurable
kausikram authored
31 #####################################################
32 ######## User Defined Values ########################
33 #####################################################
34 NAME_COLOR = "#ffffff"
35 COMPANY_COLOR = "#ff2b06"
36 ID_COLOR = "#ffffff"
37 FOLD_COLOR = "#000000"
38
39
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
40 class BadgeImage(object):
41 def __init__(self, filename):
42 self.img = Image.open(filename)
43 self.draw = ImageDraw.Draw(self.img)
44 self.width = int(self.img.size[0]*0.9)
45
46 def drawAlignedText(self, pos, text, (font, color), xtransform, ytransform):
47 width,height = font.getsize(text)
48 xpos = xtransform(pos[0], width)
49 ypos = ytransform(pos[1], height)
50 self.draw.text((xpos, ypos), text, fill=color, font=font)
51
52 def drawCenteredText(self, pos, text, font):
53 self.drawAlignedText(pos, text, font, lambda x,w:x-w/2, lambda y,h:y-h/2)
54
55 def getFitSize(self, startsize, text):
56 size = startsize
57 font = ImageFont.truetype("Trebucbd.ttf", size*300/72)
58 textwidth, textheight = font.getsize(text)
59 while textwidth > self.width:
60 size -= 1
61 font = ImageFont.truetype("Trebucbd.ttf", size*300/72)
62 textwidth, textheight = font.getsize(text)
63 return size
64
65 def drawPerson(self, name):
66 linepos = (self.img.size[0]/2, 240)
67 line1pos = (self.img.size[0]/2, 150)
68 line2pos = (self.img.size[0]/2, 320)
69 if name.find(" ") >= 0:
70 firstname, rest = name.split(" ", 1)
71 else:
72 firstname, rest = (name, "")
1a7d40d @kausikram made colours configurable
kausikram authored
73 if rest != "":
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
74 personFont = ImageFont.truetype("Trebucbd.ttf", self.getFitSize(45, firstname)*300/72)
1a7d40d @kausikram made colours configurable
kausikram authored
75 self.drawCenteredText(line1pos, firstname, (personFont, NAME_COLOR))
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
76 personFont = ImageFont.truetype("Trebucbd.ttf", self.getFitSize(45, rest)*300/72)
1a7d40d @kausikram made colours configurable
kausikram authored
77 self.drawCenteredText(line2pos, rest, (personFont, NAME_COLOR))
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
78 else:
79 personFont = ImageFont.truetype("Trebucbd.ttf", self.getFitSize(45, name)*300/72)
1a7d40d @kausikram made colours configurable
kausikram authored
80 self.drawCenteredText(linepos, name, (personFont, NAME_COLOR))
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
81
82 def drawCompany(self, name):
83 pos = (self.img.size[0]/2, 500)
84 font = ImageFont.truetype("Trebucbd.ttf", self.getFitSize(26, name)*300/72)
1a7d40d @kausikram made colours configurable
kausikram authored
85 self.drawCenteredText(pos, name, (font, COMPANY_COLOR))
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
86
87 def drawId(self, id):
88 pos = (50, 50)
89 font = ImageFont.truetype("Trebucbd.ttf", 8*300/72)
1a7d40d @kausikram made colours configurable
kausikram authored
90 self.drawCenteredText(pos, id, (font, ID_COLOR))
91
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
92
1a7d40d @kausikram made colours configurable
kausikram authored
93 def save(self, filename, doubleSided=False):
94 if not doubleSided:
95 self.img.save(filename)
96 return
97
7f736a4 @yuvipanda Changed createbadges.py from RGB to CMYK
yuvipanda authored
98 newimg = Image.new("CMYK", (self.img.size[0]*2+20, self.img.size[1]), FOLD_COLOR)
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
99 newimg.paste(self.img, (0,0))
100 newimg.paste(self.img, (self.img.size[0]+20,0))
101 newimg.save(filename)
102
1a7d40d @kausikram made colours configurable
kausikram authored
103
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
104 class DataFileReader(object):
105 def __init__(self, filename):
106 fp = open(filename)
107 self.content = fp.read()
108 fp.close()
109
110 def getData(self):
706ed9c Fixed import error and indentation error
Silver Stripe Software authored
111 reader = CsvReader(self.content)
112 reader.setColumnAlias(0, "fname")
113 reader.setColumnAlias(1, "lname")
114 reader.setColumnAlias(2, "company")
115 reader.setColumnAlias(3, "id")
116 for row in reader.rows():
117 name = row.fname + " " + row.lname
118 company = row.company
119 id = row.id
120 name = name.title()
121 # For company names that start with * we dont convert
122 # to title case. This is helpful for acronym names.
123 # Eg: IBM should not become Ibm, so we mark it as *IBM
124 # in the file
125 if not company.startswith("*"):
126 company = company.title()
127 else:
128 # If name starts with * then remove the star and
129 # take the rest as the company name
130 company = company[1:]
131 yield (id, name.title(), company)
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
132
133 import sys
134
135 if len(sys.argv) > 1:
136 filenames = sys.argv[1:]
137 else:
138 filenames = ["sample"]
139
140 count = 0
141 for filename in filenames:
142 reader = DataFileReader(filename + ".csv")
43bedd0 Fixed conflict from kausikram merge
Silver Stripe Software authored
143 if not os.path.exists(filename):
144 os.makedirs(filename)
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
145 for id, name, company in reader.getData():
146 print id, name, company
147 badge = BadgeImage("badge_template.png")
148 badge.drawPerson(name)
149 badge.drawCompany(company)
150 badge.drawId(id)
43bedd0 Fixed conflict from kausikram merge
Silver Stripe Software authored
151 badge.save(os.path.join(filename, filename + "_badge_" + str(id) + ".png"))
5fda320 Initial checkin of event badge generator
Silver Stripe Software authored
152 count += 1
153 print "\n%d badges created" % (count)
154
Something went wrong with that request. Please try again.