Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented multiline functionality
  • Loading branch information
Klaus Thoden committed Nov 29, 2017
1 parent 90dc24b commit 29438fd
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions mkimage.py
Expand Up @@ -8,6 +8,7 @@
import sys
import logging
import configparser
import textwrap
import argparse
from PIL import Image, ImageFont, ImageDraw

Expand Down Expand Up @@ -130,21 +131,6 @@ def add_watermark(image, watermarkstring):
def centered(textstring, font_spec):
"""Return coordinates for a centered string."""

# would also be good to have a function for multiline text:
# calculate width of text
# if too wide, split by whitespace
# compare width of canvas and string and determine how many lines we need
# split list of words into that many equal parts and join them
# get length of each line for centering
# determine lineskip
# return result
#
# or try and do that using multiline:
# https://pillow.readthedocs.io/en/3.3.x/reference/ImageDraw.html?highlight=multiline_text#PIL.ImageDraw.PIL.ImageDraw.Draw.multiline_text
# https://github.com/python-pillow/Pillow/issues/2067
# https://docs.python.org/3.3/library/textwrap.html?highlight=textwrap#module-textwrap


tmp_draw = ImageDraw.Draw(Image.new("RGB", DIMENSIONS, BACKGROUND))

string_width, string_height = tmp_draw.textsize(textstring, font=font_spec)
Expand All @@ -170,7 +156,7 @@ def create_cover(metadata_dict, image_directory, cover_filename):
if metadata_dict['eoa_series'].lower() == "sources":
press_text = "Edition Open Sources"
else:
press_text = "Max Planck Research Library for the\nHistory and Development of Knowledge"
press_text = "Max Planck Research Library for the History and Development of Knowledge"

if metadata_dict['eoa_series'].lower() == "textbooks":
fill_colour_top = (0, 0, 0)
Expand All @@ -183,11 +169,18 @@ def create_cover(metadata_dict, image_directory, cover_filename):

text_draw = ImageDraw.Draw(img)

# these will eventually also become candidates for multilines
text_draw.text((centered(title_text, big_bold_font), 200), title_text, font=big_bold_font, fill=fill_colour_top)
text_draw.text((centered(subtitle_text, medium_font), 350), subtitle_text, font=medium_font, fill=fill_colour_top)
text_draw.text((centered(authors_text, small_font), int(DIMENSIONS[1]/3)-200), authors_text, font=small_font, fill=fill_colour_top)
text_draw.text((centered(press_text, small_font), DIMENSIONS[1]-400), press_text, font=small_font)
text_draw.text((centered(series_number_text, small_font), DIMENSIONS[1]-200), series_number_text, font=small_font)

press_text_lines = textwrap.wrap(press_text, width=40)
press_text_lines.append(series_number_text)

press_text_joined = "\n".join(press_text_lines)
ptcenter = centered(press_text_joined, small_font)

text_draw.multiline_text((ptcenter,DIMENSIONS[1]-400), press_text_joined, font=small_font, align="center")

image_on_cover = Image.open(os.path.join(image_directory, get_cover_image(image_directory)))

Expand Down

0 comments on commit 29438fd

Please sign in to comment.