|
# retoor <retoor@molodetz.nl>
|
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image
|
|
|
|
|
|
def enforce_rgba_png(file_bytes: bytes) -> bytes:
|
|
img = Image.open(BytesIO(file_bytes)).convert("RGBA")
|
|
width, height = img.size
|
|
if width > 1 and height > 1:
|
|
corner = img.getpixel((0, 0))
|
|
if len(corner) == 4 and corner[3] == 255:
|
|
bg = corner[:3]
|
|
data = img.getdata()
|
|
cleaned = []
|
|
for pixel in data:
|
|
if pixel[:3] == bg:
|
|
cleaned.append((pixel[0], pixel[1], pixel[2], 0))
|
|
else:
|
|
cleaned.append(pixel)
|
|
img.putdata(cleaned)
|
|
buf = BytesIO()
|
|
img.save(buf, format="PNG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def resize_award_png(source: bytes, size: int) -> bytes:
|
|
img = Image.open(BytesIO(source)).convert("RGBA")
|
|
img = img.resize((size, size), Image.LANCZOS)
|
|
buf = BytesIO()
|
|
img.save(buf, format="PNG")
|
|
return buf.getvalue() |