Skip to content

Commit

Permalink
Insert paths to fonts for Docker version
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Apr 22, 2021
1 parent 41bb0c4 commit 1fd1d76
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/mkimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def format_authors(authors_list):
return authors_as_string
# def format_authors ends here

def add_watermark(image, watermarkstring):
def add_watermark(image, watermarkstring, big_red_font):
"""Add a string of text across the cover. Return a rotated image object"""

# https://codenhagen.wordpress.com/2015/12/04/putting-rotated-text-on-images-with-pillow-python/
Expand All @@ -140,7 +140,6 @@ def add_watermark(image, watermarkstring):

tmp_img = Image.new("RGBA", (DIMENSIONS[1], DIMENSIONS[0]), (0,0,0,0))
font_colour = (255,0,0)
big_red_font = ImageFont.truetype("Helvetica", 200)
text_canvas = ImageDraw.Draw(tmp_img)

text_canvas.text((0, 0), watermarkstring, font=big_red_font, fill=font_colour)
Expand Down Expand Up @@ -172,7 +171,7 @@ def centered(textstring, font_spec):
return coordinate
# def centered ends here

def create_cover(metadata_dict, image_directory, cover_filename, image_is_file):
def create_cover(metadata_dict, image_directory, cover_filename, image_is_file, big_bold_font, medium_font, small_font):
"""Create a cover using PIL"""

img = Image.new("RGB", DIMENSIONS, BACKGROUND)
Expand All @@ -198,10 +197,6 @@ def create_cover(metadata_dict, image_directory, cover_filename, image_is_file):
else:
fill_colour_top = (255, 255, 255)

big_bold_font = ImageFont.truetype(font="Times New Roman Bold", size=120)
medium_font = ImageFont.truetype(font="Times New Roman", size=100)
small_font = ImageFont.truetype(font="Times New Roman", size=80)

text_draw = ImageDraw.Draw(img)

# these will eventually also become candidates for multilines
Expand Down Expand Up @@ -256,8 +251,20 @@ def main():
parser.add_argument("-c", "--config", help="File that contains the publication data.", default="publication.cfg")
parser.add_argument("-o", "--output", help="Name of output file.", default="Cover.jpg")
parser.add_argument("-nw", "--nowatermark", help="Do not add a watermark to the image.", action="store_true")
parser.add_argument("-lf", "--localfonts", help="Use local fonts, rather than those used in Docker", action="store_true")
args = parser.parse_args()

if args.localfonts:
big_red_font = ImageFont.truetype("Helvetica", 200)
big_bold_font = ImageFont.truetype(font="Times New Roman Bold", size=120)
medium_font = ImageFont.truetype(font="Times New Roman", size=100)
small_font = ImageFont.truetype(font="Times New Roman", size=80)
else:
big_red_font = ImageFont.truetype("/usr/share/fonts/truetype/msttcorefonts/comic.ttf", 200)
big_bold_font = ImageFont.truetype(font="/usr/share/fonts/truetype/msttcorefonts/timesbd.ttf", size=120)
medium_font = ImageFont.truetype(font="/usr/share/fonts/truetype/msttcorefonts/times.ttf", size=100)
small_font = ImageFont.truetype(font="/usr/share/fonts/truetype/msttcorefonts/times.ttf", size=80)

if args.config:
# if os.path.exists("publication.cfg"):
logging.info("Using %s as publication config" % (args.config))
Expand Down Expand Up @@ -294,12 +301,12 @@ def main():
print("No valid image or directory given. Exiting.")
sys.exit()

create_cover(METADATA_DICT, IMAGES, OUTFILE, image_is_file)
create_cover(METADATA_DICT, IMAGES, OUTFILE, image_is_file, big_bold_font, medium_font, small_font)

if args.nowatermark:
logging.info("No watermark. Be careful. This is not meant to be an official cover.")
else:
add_watermark(OUTFILE, "Automatically generated cover.\nDo not use in production!")
add_watermark(OUTFILE, "Automatically generated cover.\nDo not use in production!", big_red_font)
# def main ends here

if __name__ == '__main__':
Expand Down

0 comments on commit 1fd1d76

Please sign in to comment.