Search code examples
pythondiscorddiscord.pypython-imaging-library

Python pillow generates image wrong


So im trying to make a rank card using python and pillow.

everything works with a black background.

this is what it looks like with a black background: rankcard

the background: enter image description here

but when i add a color to the background for example blue then the image looks really weird: broken rankcard

the background: enter image description here

this is the code i use for generating the rankcard

def rankcardbuilder(username, level, xpgot, xpneed, color):
    try:
        # Get Images
        background = Image.open("assets/test.png")
        mask = Image.open("assets/largemask.png").convert("L")
        avatar = Image.open("assets/avatar.png")

        # Get Local Font
        font = ImageFont.truetype("assets/Comfortaa-Bold.ttf", 46)
        fontsmall = ImageFont.truetype("assets/Comfortaa-Bold.ttf", 35)
        fontsmaller = ImageFont.truetype("assets/Comfortaa-Bold.ttf", 18)

        # Add Text
        finalimage = ImageDraw.Draw(background)
        finalimage.text((320, 95), username, ("white"), font=font)
        finalimage.text((320, 155), f"Level {level} Rank #1", ("white"), font=fontsmall)
        finalimage.text((425, 200), f"{xpgot:,}/{xpneed:,} XP", ("white"), font=fontsmaller)

        # Round & Place the avatar
        roundavatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
        roundavatar.putalpha(mask)
        background.paste(roundavatar, (40, 45), roundavatar)

        # Generate Progress Bar
        bar = Image.open('assets/progresssmall.png').convert('RGB')
        draw = ImageDraw.Draw(bar)

        color = (255, 255, 255)

        x = 0

        y, diam = 4, 17

        draw.ellipse([x, y, x + diam, y + diam], fill=color)

        ImageDraw.floodfill(bar, xy=(7, 12), value=color, thresh=40)

        background.paste(bar, (314, 220))


        # Save generated Rankcard
        background.save("assets/rankcard.png")

am i doing something wrong?


Solution

  • It looks like your background image is palettized. You don't want that; you should convert to RGBA.