From dafbe96df886e0e4f549af18af26e1fe43efdd15 Mon Sep 17 00:00:00 2001 From: Klaus Thoden Date: Tue, 29 May 2018 16:40:08 +0200 Subject: [PATCH] Additional command line options Support for single file --- mkimage.py | 57 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/mkimage.py b/mkimage.py index e936228..f0ff387 100644 --- a/mkimage.py +++ b/mkimage.py @@ -12,7 +12,7 @@ import argparse from PIL import Image, ImageFont, ImageDraw -logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s') +logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s') DIMENSIONS = (2000, 2844) # ratio of 0.703205791106515 BACKGROUND = 0, 0, 0 @@ -35,6 +35,9 @@ def get_cover_image(image_path): candidates = os.listdir(image_path) for image in candidates: + if image == ".DS_Store": + candidates.remove(image) + continue tmp_image = Image.open(image_path + "/" + str(image)) ratio = calculate_ratio(tmp_image) if ratio < 1: @@ -124,7 +127,7 @@ def add_watermark(image, watermarkstring): base_image.paste(slanted_image, (200, 100), slanted_image) base_image.save(image) - print("Added a watermark to", image) + logging.info("Added a watermark to %s." % image) # return slanted_image # def add_watermark ends here @@ -140,7 +143,7 @@ def centered(textstring, font_spec): return coordinate # def centered ends here -def create_cover(metadata_dict, image_directory, cover_filename): +def create_cover(metadata_dict, image_directory, cover_filename, image_is_file): """Create a cover using PIL""" img = Image.new("RGB", DIMENSIONS, BACKGROUND) @@ -151,6 +154,9 @@ def create_cover(metadata_dict, image_directory, cover_filename): title_text = metadata_dict['eoa_title'] subtitle_text = metadata_dict['eoa_subtitle'] authors_text = format_authors(metadata_dict['eoa_authors']) + if len(metadata_dict['eoa_zusatz']) > 0: + authors_text += " {}".format(metadata_dict['eoa_zusatz']) + series_number_text = "{0} {1}".format(metadata_dict['eoa_series'], metadata_dict['eoa_number']) if metadata_dict['eoa_series'].lower() == "sources": @@ -207,40 +213,57 @@ def create_cover(metadata_dict, image_directory, cover_filename): img.paste(resized_image, (int(coord), 1200)) img.save(cover_filename) - print("Wrote", cover_filename) - - watermark = add_watermark(cover_filename, "Automatically generated cover.\nDo not use in production!") + logging.info("Wrote %s." % cover_filename) # def create_cover ends here if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument("image_dir", help="Path to directory with potential images.") + parser.add_argument("image_dir_or_image", help="Path to directory with potential images or a single file.") 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") args = parser.parse_args() - IMAGES = args.image_dir - - if os.path.exists("publication.cfg"): - logging.debug("Using %s as publication config" % (args.config)) + if args.config: + # if os.path.exists("publication.cfg"): + logging.info("Using %s as publication config" % (args.config)) config = configparser.ConfigParser() config.read(args.config) - list_of_authors = list(set(config['Authors'].values())) - for author in list_of_authors: - if len(author) == 0: - list_of_authors.remove(author) + list_of_authors = [] + + for entry in range(0, 5): + author_label = "Author" + str(entry + 1) + tmp_author = config['Authors'][author_label] + if len(tmp_author) > 0: + list_of_authors.append(tmp_author) METADATA_DICT.update({'eoa_series' : config['Technical']['Serie']}) METADATA_DICT.update({'eoa_number' : config['Technical']['Number']}) METADATA_DICT.update({'eoa_title' : config['Technical']['Title']}) METADATA_DICT.update({'eoa_subtitle' : config['Technical']['Subtitle']}) METADATA_DICT.update({'eoa_authors' : list_of_authors}) + METADATA_DICT.update({'eoa_zusatz' : config['Authors']['Zusatz']}) else: - logging.debug("Using the built-in metadata as publication config") + logging.info("Using the built-in metadata as publication config") OUTFILE = args.output - create_cover(METADATA_DICT, IMAGES, OUTFILE) + IMAGES = args.image_dir_or_image + + if os.path.isfile(IMAGES): + image_is_file = True + elif os.path.isdir(IMAGES): + image_is_file = False + else: + print("No valid image or directory given. Exiting.") + sys.exit() + + create_cover(METADATA_DICT, IMAGES, OUTFILE, image_is_file) + + 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!") # finis