remove outputs from module

This commit is contained in:
Denis Lehmann 2021-07-18 21:51:35 +02:00
parent 66e763f0d0
commit 138d663cfc
2 changed files with 73 additions and 67 deletions

View File

@ -7,9 +7,9 @@ from huepaper import (
add_lines, add_lines,
add_pixelation, add_pixelation,
add_emblem, add_emblem,
save_image,
) )
import argparse import argparse
import os
def print_greeter(): def print_greeter():
@ -28,6 +28,38 @@ def print_greeter():
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__": if __name__ == "__main__":
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -152,34 +184,45 @@ if __name__ == "__main__":
parser.error("Pixelation value must be set in form: 42x42") parser.error("Pixelation value must be set in form: 42x42")
print_greeter() 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: try:
image = add_lines(image, base_color.rgb + (lines,)) random_color = False if color else True
if lines_bright: base_color = get_base_color(color, sat_min, sat_max, lum_min, lum_max)
image = add_lines(image, (1.0, 1.0, 1.0, lines_bright)) if random_color:
if lines_dark: print("Selected random base color: {}".format(base_color.hex))
image = add_lines(image, (0.0, 0.0, 0.0, lines_dark))
if pixelate: c1, c2, c3, c4 = create_colors(
image = add_pixelation(image, px, py) base_color, hue_max, sat_min, sat_max, lum_min, lum_max
)
if emblem: image = create_base_image(c1, c2, c3, c4, width, height)
image = add_emblem(image, emblem)
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: if pixelate:
image.show() image = add_pixelation(image, px, py)
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: if emblem:
save_image(image, output) 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)

View File

@ -2,7 +2,6 @@
from PIL import Image, ImageDraw, ImageOps from PIL import Image, ImageDraw, ImageOps
from colour import Color from colour import Color
import os.path
import random import random
@ -16,7 +15,6 @@ def get_base_color(
sat = random.uniform(sat_min, sat_max) sat = random.uniform(sat_min, sat_max)
lum = random.uniform(lum_min, lum_max) lum = random.uniform(lum_min, lum_max)
base_color = Color(hue=hue, saturation=sat, luminance=lum) base_color = Color(hue=hue, saturation=sat, luminance=lum)
print("Selected random base color: {}".format(base_color.hex))
# Else try to parse string # Else try to parse string
else: else:
@ -26,14 +24,13 @@ def get_base_color(
try: try:
base_color = Color("#{}".format(color_string)) base_color = Color("#{}".format(color_string))
except: except:
print("Invalid color expression: {}".format(color_string)) raise Exception("Invalid color expression: {}".format(color_string))
exit(1)
return base_color return base_color
def create_colors( 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.""" """Create four corner colors for a huepaper by an optional base color."""
if not base_color: if not base_color:
@ -160,13 +157,11 @@ def add_emblem(image, filepath):
try: try:
emblem_image = Image.open(filepath) emblem_image = Image.open(filepath)
except Exception as e: except Exception as e:
print("Failed to load emblem: {}".format(e)) raise Exception("Failed to load emblem: {}".format(e))
exit(1)
# Exit if emblem is too big # Exit if emblem is too big
if emblem_image.size[0] > width or emblem_image.size[1] > height: if emblem_image.size[0] > width or emblem_image.size[1] > height:
print("Emblem can't be bigger than the wallpaper") raise Exception("Emblem can't be bigger than the huepaper")
exit(1)
# Insert emblem in the center # Insert emblem in the center
offset = ( offset = (
@ -176,35 +171,3 @@ def add_emblem(image, filepath):
image.alpha_composite(emblem_image, offset) image.alpha_composite(emblem_image, offset)
return image 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: "
)