Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- from dotenv import load_dotenv
- import os
- from PIL import Image
- from io import BytesIO
- load_dotenv()
- TOKEN = os.environ.get("TOKEN")
- client = commands.Bot(command_prefix="!", intents=discord.Intents.default())
- tree = client.tree
- @client.event
- async def on_ready():
- print(f"Logged in as {client.user}")
- synced = await tree.sync()
- print(f"Synced {len(synced)} Command(s).")
- new_size = 32
- COLORS = {
- (49, 55, 61): "⬛",
- (85, 172, 238): "🟦",
- (221, 46, 68): "🟥",
- (253, 203, 88): "🟨",
- (193, 105, 79): "🟫",
- (244, 144, 12): "🟧",
- (170, 142, 214): "🟪",
- (230, 231, 232): "⬜",
- (120, 177, 89): "🟩",
- }
- @tree.command(name="emojify", description="converts image into emojis of pixels")
- async def _emojify(interaction: discord.Interaction, image: discord.Attachment):
- await interaction.response.defer()
- with BytesIO(await image.read()) as file:
- resized = Image.open(file).resize((new_size, new_size), resample=Image.NEAREST).convert("RGB")
- msg = convertPixels(resized)
- await interaction.followup.send(f"```{msg}```")
- def convertPixels(img: Image.Image):
- pixelsArray = img.load()
- emojified = ""
- width, height = img.size
- for row in range(height):
- for column in range(width):
- nearestColor = getNearestColor(pixelsArray[column, row])
- emojified += nearestColor
- emojified += '\n'
- return emojified
- def getNearestColor(pixels: tuple):
- distancesList = []
- for refPixels, color in COLORS.items():
- r1, g1, b1 = refPixels
- r2, g2, b2 = pixels
- distance = ((r2-r1)**2 + (g2-g1)**2 + (b2-b1)**2) ** 0.5
- distancesList.append(distance)
- sortedList = sorted(distancesList)
- idx = distancesList.index(sortedList[0])
- return list(COLORS.values())[idx]
- client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement