From 1fd1d76f77f08dd89b50dbf4f619174283d065cc Mon Sep 17 00:00:00 2001 From: kthoden Date: Thu, 22 Apr 2021 10:39:18 +0200 Subject: [PATCH] Insert paths to fonts for Docker version --- src/mkimage.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/mkimage.py b/src/mkimage.py index 1d6b39a..5fe07d9 100755 --- a/src/mkimage.py +++ b/src/mkimage.py @@ -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/ @@ -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) @@ -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) @@ -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 @@ -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)) @@ -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__':