im trying to make a quote command, but the grey mask part keeps showing
import discord
from discord.ext import commands
from PIL import Image
import io
class Quote(commands.Cog, name="Quote"):
def __init__(self, bot: commands.Bot):
self.bot = bot
# Load the overlay image
self.quote_overlay = Image.open("quote_overlay.png").convert("RGBA")
@discord.app_commands.command(name="quote", description="Add a quote overlay to an image")
async def quote(self, interaction: discord.Interaction, image: discord.Attachment):
try:
# Download and process the image
image_bytes = await image.read()
img = Image.open(io.BytesIO(image_bytes)).convert("RGBA")
# Resize the original image to match the overlay width while maintaining aspect ratio
overlay_width = self.quote_overlay.width
aspect_ratio = img.height / img.width
new_img_height = int(overlay_width * aspect_ratio)
img = img.resize((overlay_width, new_img_height), Image.LANCZOS)
# Resize the overlay to 20% height of the resized original
new_height = int(img.size[1] * 0.2)
overlay_resized = self.quote_overlay.resize((img.size[0], new_height), Image.LANCZOS)
# Create a blank image for the result
result = Image.new('RGBA', img.size, (0, 0, 0, 0))
result.paste(img, (0, 0))
# Create an all-transparent overlay mask
overlay_mask = overlay_resized.split()[3] # Get the alpha channel
fully_transparent_overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
fully_transparent_overlay.paste(overlay_resized, (0, 0), overlay_mask)
# Composite the original with the transparent overlay
result = Image.alpha_composite(result, fully_transparent_overlay)
# Save result to buffer
buffer = io.BytesIO()
result.save(buffer, format="PNG")
buffer.seek(0)
# Send result
await interaction.response.send_message(
file=discord.File(buffer, filename="quoted_image.png")
)
except Exception as e:
await interaction.response.send_message(
f"Error processing image: {str(e)}",
ephemeral=True
)
async def setup(client):
await client.add_cog(Quote(client), guilds=[discord.Object(id=879166421328871464)])