Now alpha channel turns white. Will it work first try?
This commit is contained in:
parent
4392e811e6
commit
d7fd4cd226
15
app.py
15
app.py
@ -36,6 +36,19 @@ def remove_uploaded_img():
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def remove_alpha(img):
|
||||||
|
if img.mode in ("RGBA", "LA"):
|
||||||
|
background = Image.new("RGBA", img.size, (255, 255, 255, 255)) # White background
|
||||||
|
img = Image.alpha_composite(background, img.convert("RGBA"))
|
||||||
|
return img.convert("RGB")
|
||||||
|
elif img.mode == "P" and 'transparency' in img.info:
|
||||||
|
img = img.convert("RGBA")
|
||||||
|
background = Image.new("RGBA", img.size, (255, 255, 255, 255))
|
||||||
|
img = Image.alpha_composite(background, img)
|
||||||
|
return img.convert("RGB")
|
||||||
|
else:
|
||||||
|
return img
|
||||||
|
|
||||||
def bleh_image_from_url(url, dithering, mode):
|
def bleh_image_from_url(url, dithering, mode):
|
||||||
resp = requests.get(url, stream=True)
|
resp = requests.get(url, stream=True)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
@ -46,6 +59,7 @@ def bleh_image_from_url(url, dithering, mode):
|
|||||||
if bleh.returncode != 0:
|
if bleh.returncode != 0:
|
||||||
raise RuntimeError(f"Driver failed: {err.decode()}")
|
raise RuntimeError(f"Driver failed: {err.decode()}")
|
||||||
img = Image.open(io.BytesIO(out)).convert("L")
|
img = Image.open(io.BytesIO(out)).convert("L")
|
||||||
|
img = remove_alpha(img)
|
||||||
# Optionally check width, pad/resize if needed
|
# Optionally check width, pad/resize if needed
|
||||||
if img.width != IMAGE_WIDTH:
|
if img.width != IMAGE_WIDTH:
|
||||||
img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS)
|
img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS)
|
||||||
@ -60,6 +74,7 @@ def bleh_image_from_bytes(image_bytes, dithering, mode):
|
|||||||
if bleh.returncode != 0:
|
if bleh.returncode != 0:
|
||||||
raise RuntimeError(f"Driver failed: {err.decode()}")
|
raise RuntimeError(f"Driver failed: {err.decode()}")
|
||||||
img = Image.open(io.BytesIO(out)).convert("L")
|
img = Image.open(io.BytesIO(out)).convert("L")
|
||||||
|
img = remove_alpha(img)
|
||||||
if img.width != IMAGE_WIDTH:
|
if img.width != IMAGE_WIDTH:
|
||||||
img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS)
|
img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS)
|
||||||
return img
|
return img
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user