diff --git a/app.py b/app.py index 5b6ca61..bf2eeed 100644 --- a/app.py +++ b/app.py @@ -52,31 +52,40 @@ def remove_alpha(img): def bleh_image_from_url(url, dithering, mode): resp = requests.get(url, stream=True) resp.raise_for_status() + img = Image.open(io.BytesIO(resp.content)) + img = remove_alpha(img) + buf = io.BytesIO() + img.save(buf, format="PNG") + image_bytes_no_alpha = buf.getvalue() bleh = subprocess.Popen( ["./bleh", "-o", "-", "-mode", f"{mode}", "-d", f"{dithering}", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = bleh.communicate(resp.content) + out, err = bleh.communicate(image_bytes_no_alpha) if bleh.returncode != 0: raise RuntimeError(f"Driver failed: {err.decode()}") - img = Image.open(io.BytesIO(out)) - img = remove_alpha(img) - img = img.convert("L") + img = Image.open(io.BytesIO(out)).convert("L") # Optionally check width, pad/resize if needed if img.width != IMAGE_WIDTH: img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS) return img + def bleh_image_from_bytes(image_bytes, dithering, mode): + # OPEN the uploaded image and remove alpha + img = Image.open(io.BytesIO(image_bytes)) + img = remove_alpha(img) + buf = io.BytesIO() + img.save(buf, format="PNG") + image_bytes_no_alpha = buf.getvalue() + # pass that to bleh bleh = subprocess.Popen( ["./bleh", "-o", "-", "-mode", f"{mode}", "-d", f"{dithering}", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) - out, err = bleh.communicate(image_bytes) + out, err = bleh.communicate(image_bytes_no_alpha) if bleh.returncode != 0: raise RuntimeError(f"Driver failed: {err.decode()}") - img = Image.open(io.BytesIO(out)) - img = remove_alpha(img) - img = img.convert("L") + img = Image.open(io.BytesIO(out)).convert("L") if img.width != IMAGE_WIDTH: img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS) return img