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