Skip to content

Commit

Permalink
add basic svg image generation support, refactor constants to be uppe…
Browse files Browse the repository at this point in the history
…rcase
  • Loading branch information
kronicka committed Jan 12, 2019
1 parent 47fd702 commit 1774fef
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
30 changes: 19 additions & 11 deletions utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

# Constants
# Life Expectancy (2016 WB Data)
general_life_expectancy_days = 26097.5
general_life_expectancy_weeks = 3728.214 # ~71.5 years
female_life_expectancy_weeks = 3872.18071 # ~74.261 years
male_life_expectancy_weeks = 3647.54929 # ~69.953 years
GENERAL_LIFE_EXPECTANCY_DAYS = 26097.5
GENERAL_LIFE_EXPECTANCY_WEEKS = 3728.214 # ~71.5 years
FEMALE_LIFE_EXPECTANCY_WEEKS = 3872.18071 # ~74.261 years
MALE_LIFE_EXPECTANCY_WEEKS = 3647.54929 # ~69.953 years

# Image Paths
square_path = 'img/square.jpg'
background_path = 'img/background.png'
SQUARE_PATH = 'img/square.jpg'
BACKGROUND_PATH = 'img/background.png'

# Text Draw Utilities
dark_grey = (192, 192, 192)
macos_font_path = '/Library/Fonts/Arial.ttf'
windows_font_path = 'arial.ttf'
path = windows_font_path if system() == 'Windows' else macos_font_path
font = ImageFont.truetype(path, 24)
DARK_GREY = (192, 192, 192)
MACOS_FONT_PATH = '/Library/Fonts/Arial.ttf'
WINDOWS_FONT_PATH = 'arial.ttf'
PATH = WINDOWS_FONT_PATH if system() == 'Windows' else MACOS_FONT_PATH
FONT = ImageFont.truetype(PATH, 24)

# SVG calendar generation constants
CAL_WIDTH = '420mm'
CAL_HEIGHT = '594mm'
CAL_SIZE = (CAL_WIDTH, CAL_HEIGHT)
CSS_STYLES = """
.square { fill: white; stroke: black; stroke-width: .1mm; padding: .2mm; }
"""
4 changes: 2 additions & 2 deletions utils/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def draw_text(img: Image) -> None:
padding_left = 226 # The almost exact estimated half of pixel width of the current default tagline
padding_bottom = 40
draw.text(((img.size[0] / 2) - padding_left, img.size[1] - padding_bottom),
text, constants.dark_grey, font=constants.font)
text, constants.DARK_GREY, font=constants.FONT)


def draw_units_number(img: Image, units: int, unit_type: str) -> None:
Expand All @@ -24,4 +24,4 @@ def draw_units_number(img: Image, units: int, unit_type: str) -> None:
padding_left = 170
padding_bottom = 40
draw.text((img.size[0] - padding_left, img.size[1] - padding_bottom),
text, constants.dark_grey, font=constants.font)
text, constants.DARK_GREY, font=constants.FONT)
43 changes: 43 additions & 0 deletions utils/draw_svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import svgwrite
from utils.constants import CAL_SIZE, CSS_STYLES


def generate_calendar_svg(units: int, unit_type: str = 'weeks'):
"""
Generate an svg calendar based on the number of weeks
"""
dwg = svgwrite.Drawing('calendar.svg', size=CAL_SIZE)
dwg.viewbox(0, 0, 420, 594)
dwg.defs.add(dwg.style(CSS_STYLES))

dwg.add(dwg.rect(size=('100%', '100%'), fill='white', class_='background'))

def group(classname):
return dwg.add(dwg.g(class_=classname))
squares = group('square')

square_size = (8, 8)
cols = 40

if unit_type == 'days':
square_size = (1, 1)
cols = 240

rows, leftover = divmod(units / cols, 1)
rows = int(rows) + 1

for row in range(0, rows):
if row == rows - 1 and leftover != 0:
cols *= leftover
cols = int(cols)
for col in range(0, cols):
xc = col * 10 + 1
yc = row * 10 + 1
square = dwg.rect(insert=(xc, yc), size=square_size)
squares.add(square)

dwg.save()


if __name__ == '__main__':
generate_calendar_svg(100)

0 comments on commit 1774fef

Please sign in to comment.