From 138d663cfc1aff7f3cfc07facd615d82cd5ad880 Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Sun, 18 Jul 2021 21:51:35 +0200 Subject: [PATCH] remove outputs from module --- huepaper.py | 95 ++++++++++++++++++++++++++++++++------------ huepaper/__init__.py | 45 ++------------------- 2 files changed, 73 insertions(+), 67 deletions(-) diff --git a/huepaper.py b/huepaper.py index 2118251..1d53826 100755 --- a/huepaper.py +++ b/huepaper.py @@ -7,9 +7,9 @@ from huepaper import ( add_lines, add_pixelation, add_emblem, - save_image, ) import argparse +import os def print_greeter(): @@ -28,6 +28,38 @@ def print_greeter(): print(greeter) +def save_image(image, filepath): + """Save an image at given filepath.""" + save = True + + # Check whether file exists + if os.path.isfile(filepath): + overwrite = input( + "The file {} already exists. Do you want to overwrite it? [y/N] ".format( + filepath + ) + ) + if overwrite != "y" and overwrite != "Y": + save = False + + if save: + + stop = False + while not stop: + try: + image.save(filepath) + stop = True + except Exception as e: + print("Failed to save wallpaper: {}".format(e)) + again = input("Do you want to try again? [Y/n] ") + if again == "n" or again == "N": + stop = True + else: + filepath = input( + "Please enter new path where the wallpaper shall be saved: " + ) + + if __name__ == "__main__": parser = argparse.ArgumentParser( @@ -152,34 +184,45 @@ if __name__ == "__main__": parser.error("Pixelation value must be set in form: 42x42") print_greeter() - base_color = get_base_color(color, sat_min, sat_max, lum_min, lum_max) - c1, c2, c3, c4 = create_colors( - base_color, hue_max, sat_min, sat_max, lum_min, lum_max - ) - image = create_base_image(c1, c2, c3, c4, width, height) - if lines: - image = add_lines(image, base_color.rgb + (lines,)) - if lines_bright: - image = add_lines(image, (1.0, 1.0, 1.0, lines_bright)) - if lines_dark: - image = add_lines(image, (0.0, 0.0, 0.0, lines_dark)) + try: + random_color = False if color else True + base_color = get_base_color(color, sat_min, sat_max, lum_min, lum_max) + if random_color: + print("Selected random base color: {}".format(base_color.hex)) - if pixelate: - image = add_pixelation(image, px, py) + c1, c2, c3, c4 = create_colors( + base_color, hue_max, sat_min, sat_max, lum_min, lum_max + ) - if emblem: - image = add_emblem(image, emblem) + image = create_base_image(c1, c2, c3, c4, width, height) - image.mode = "RGB" + if lines: + image = add_lines(image, base_color.rgb + (lines,)) + if lines_bright: + image = add_lines(image, (1.0, 1.0, 1.0, lines_bright)) + if lines_dark: + image = add_lines(image, (0.0, 0.0, 0.0, lines_dark)) - if not no_preview: - image.show() - if not output: - save = input("Do you want to save the image? [y/N] ") - if save == "y" or save == "Y": - path = input("Enter the path where the wallpaper shall be saved: ") - save_image(image, path) + if pixelate: + image = add_pixelation(image, px, py) - if output: - save_image(image, output) + if emblem: + image = add_emblem(image, emblem) + + image.mode = "RGB" + + if not no_preview: + image.show() + if not output: + save = input("Do you want to save the image? [y/N] ") + if save == "y" or save == "Y": + path = input("Enter the path where the wallpaper shall be saved: ") + save_image(image, path) + + if output: + save_image(image, output) + + except Exception as e: + print(str(e)) + exit(1) diff --git a/huepaper/__init__.py b/huepaper/__init__.py index 6ea1a67..e34f068 100644 --- a/huepaper/__init__.py +++ b/huepaper/__init__.py @@ -2,7 +2,6 @@ from PIL import Image, ImageDraw, ImageOps from colour import Color -import os.path import random @@ -16,7 +15,6 @@ def get_base_color( sat = random.uniform(sat_min, sat_max) lum = random.uniform(lum_min, lum_max) base_color = Color(hue=hue, saturation=sat, luminance=lum) - print("Selected random base color: {}".format(base_color.hex)) # Else try to parse string else: @@ -26,14 +24,13 @@ def get_base_color( try: base_color = Color("#{}".format(color_string)) except: - print("Invalid color expression: {}".format(color_string)) - exit(1) + raise Exception("Invalid color expression: {}".format(color_string)) return base_color def create_colors( - base_color=None, hue_max=0.1, sat_min=0.2, sat_max=1.0, lum_min=0.2, lum_max=0.9 + base_color=None, hue_max=0.1, sat_min=0.2, sat_max=1.0, lum_min=0.3, lum_max=0.9 ): """Create four corner colors for a huepaper by an optional base color.""" if not base_color: @@ -160,13 +157,11 @@ def add_emblem(image, filepath): try: emblem_image = Image.open(filepath) except Exception as e: - print("Failed to load emblem: {}".format(e)) - exit(1) + raise Exception("Failed to load emblem: {}".format(e)) # Exit if emblem is too big if emblem_image.size[0] > width or emblem_image.size[1] > height: - print("Emblem can't be bigger than the wallpaper") - exit(1) + raise Exception("Emblem can't be bigger than the huepaper") # Insert emblem in the center offset = ( @@ -176,35 +171,3 @@ def add_emblem(image, filepath): image.alpha_composite(emblem_image, offset) return image - - -def save_image(image, filepath): - """Save an image at given filepath.""" - save = True - - # Check whether file exists - if os.path.isfile(filepath): - overwrite = input( - "The file {} already exists. Do you want to overwrite it? [y/N] ".format( - filepath - ) - ) - if overwrite != "y" and overwrite != "Y": - save = False - - if save: - - stop = False - while not stop: - try: - image.save(filepath) - stop = True - except Exception as e: - print("Failed to save wallpaper: {}".format(e)) - again = input("Do you want to try again? [Y/n] ") - if again == "n" or again == "N": - stop = True - else: - filepath = input( - "Please enter new path where the wallpaper shall be saved: " - )