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 %} LabelPrinter Image upload + + {% endblock %} {% block content %} -

Upload Image

-
+ {% if label_width == 450 %}
+
+
+ Advanced options + Dithering:
+
+ Black/white cutoff threshold: % +
+

Recommended image height = {{label_width}}, other sizes will be scaled @@ -51,20 +68,57 @@ return localStorage.getItem(name) } - dropdown_value = get_persistent("image_preset_dropdown", "file") - document.getElementById("image_preset_dropdown").value = dropdown_value - handle_dropdown(dropdown_value) - function handle_dropdown(value) { localStorage.setItem("image_preset_dropdown", value) - console.log() if (value == "file") { document.getElementById("file").style.display = "inline"; } else { document.getElementById("file").style.display = "none"; } } + + + const persistent_elements = ["dropdown", "threshold", "dithering", "advanced_options"] + + for (element_name of persistent_elements) { + if (localStorage.getItem("image_form " + element_name) == null) { + continue; + } + + element = document.getElementsByName(element_name)[0] + + if (element.type == 'checkbox') { + element.checked = localStorage.getItem("image_form " + element_name) === "true"; + } else if (element.name == 'advanced_options') { + element.open = localStorage.getItem("image_form " + element_name) === "true"; + } else { + element.value = localStorage.getItem("image_form " + element_name); + } + + if (element.name == 'dropdown') { + handle_dropdown(element.value); + } + } + + const image_form = document.getElementById("image_form") + image_form.addEventListener('submit', function(e) { + e.preventDefault(); + + for (const element of image_form.elements) { + if (element.type == 'checkbox') { + localStorage.setItem("image_form " + element.name, element.checked); + } else { + localStorage.setItem("image_form " + element.name, element.value); + } + } + + element = document.getElementsByName("advanced_options")[0] + localStorage.setItem("image_form " + element.name, element.open); + + image_form.submit(); + }) + {% endblock %}