Fixed the fix. We'll pretend that never happened.

This commit is contained in:
Ignacio Rivero 2025-06-26 21:30:54 -03:00
parent b6223caa80
commit 072f70c7aa

25
app.py
View File

@ -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