Skip to content

Instantly share code, notes, and snippets.

@FlamptX
Last active May 31, 2021 19:56
Show Gist options
  • Save FlamptX/7778d936b9d055ee9919bebd9ecd5fbc to your computer and use it in GitHub Desktop.
Save FlamptX/7778d936b9d055ee9919bebd9ecd5fbc to your computer and use it in GitHub Desktop.
Get the most dominant color in the member's profile picture and set it as the embed color so it fits with the image.
from discord.ext import commands
import binascii
from PIL import Image
from io import BytesIO
import numpy as np
import scipy.cluster
bot = commands.Bot(command_prefix="!")
@bot.command()
async def test(ctx):
NUM_CLUSTERS = 5
data = await ctx.author.avatar_url_as().read()
stream = BytesIO(data)
im = Image.open(stream)
ar = np.asarray(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float)
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
index_max = scipy.argmax(counts) # find most frequent
peak = codes[index_max]
colour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii')
embed = discord.Embed(title=ctx.author.name, description="Info here probably", color=int(colour, 16))
embed.set_thumbnail(url=ctx.author.avatar_url)
await ctx.send(embed=embed)
bot.run("TOKEN duh")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment