diff --git a/app.py b/app.py index 231f5cf..279d7d7 100644 --- a/app.py +++ b/app.py @@ -46,9 +46,15 @@ def on_new_user(session): session["cut"] = True session["fonts"] = fonts() +class Image_preset: + def __init__(self, path, dithering=True, threshold_if_not_dithering=0.5): + self.path = path + self.dithering = dithering + self.threshold_if_not_dithering = threshold_if_not_dithering + image_presets = { - "Abandoned": "resources/abandoned.jpg" + "Abandoned": Image_preset("resources/abandoned.jpg", False, 0.5) } @app.route('/image', methods=['GET', 'POST']) @@ -59,17 +65,18 @@ def image(): if 'image' not in request.files: return render_image_template() - if "dropdown" in request.form and request.form["dropdown"] != "file": - path = image_presets[request.form["dropdown"]] + path = image_presets[request.form["dropdown"]].path + dither = image_presets[request.form["dropdown"]].dithering + threshold_if_not_dithering = image_presets[request.form["dropdown"]].threshold_if_not_dithering class file: filename = os.path.basename(path) stream = open(path, "rb") else: file = request.files['image'] - - print(file.stream) + dither = "dithering" in request.form + threshold_if_not_dithering = float(request.form["threshold"]) * 0.01 if file.filename == '': @@ -78,7 +85,7 @@ def image(): extension = os.path.splitext(file.filename)[1] try: - message, status, img = process_image(file.stream) + message, status, img = process_image(file.stream, dither, threshold_if_not_dithering) file.stream.close() session["uploaded image path"] = uploaded_image_filename img.save(get_file_path(session, session["uploaded image path"])) diff --git a/process_image.py b/process_image.py index 2b60b76..7d8bc0e 100644 --- a/process_image.py +++ b/process_image.py @@ -11,7 +11,7 @@ def format_image_to_label(path): return new_image -def process_image(image_filestream): +def process_image(image_filestream, dither: bool, threshold_if_not_dithering: float): image = Image.open(image_filestream) message = None status = None @@ -26,7 +26,15 @@ def process_image(image_filestream): image = image.resize((new_width, new_height)) - image = image.convert('1', dither=Image.FLOYDSTEINBERG) + if dither: + image = image.convert('1', dither=Image.FLOYDSTEINBERG) + else: + threshold = int(threshold_if_not_dithering * 255) + print("thesholding") + image = image.point(lambda p: 255 if p > threshold else 0) + print("done thesholding") + image = image.convert("1") + if image.width > max_label_length: message= f"Label too long, max allowed length = {max_label_length}, yours = {image.width}." diff --git a/templates/image.html b/templates/image.html index aa913d7..27180cd 100644 --- a/templates/image.html +++ b/templates/image.html @@ -3,11 +3,20 @@ {% block head %}