I am currently trying to draw a progress bar, based on a calculated percentage.
However, I am not able to display it in the right format.
I orientated myself on another answer from this site here (How do you make a progress bar and put it on an image? & Is it possible to add a blue bar using PIL or Pillow?)
But either the case is that the progress bar is too long and the width
restriction does not work or the bar shows no progress.
Example 1:
async def rank(self, ctx, member: discord.Member):
member = ctx.author
data = await database.find_user(collection_user, ctx.guild.id, ctx.author.id)
already_earned = data["exp"]
to_reach= ((50 * (data['lvl'] ** 2)) + (50 * (data['lvl'] - 1)))
percentage = ((data["exp"] / next_level_xp ) * 100) # Get the percentage
## Rank card
img = Image.open("leveling/rank.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("settings/myfont.otf", 35)
font1 = ImageFont.truetype("settings/myfont.otf", 24)
async with aiohttp.ClientSession() as session:
async with session.get(str(ctx.author.avatar)) as response:
image = await response.read()
icon = Image.open(BytesIO(image)).convert("RGBA")
img.paste(icon.resize((156, 156)), (50, 60))
# Some text drawn, this works
### From StackOverflow ###
def drawProgressBar(d, x, y, w, h, progress, bg=(129, 66, 97), fg=(211,211,211)):
# draw background
draw.ellipse((x+w, y, x+h+w, y+h), fill=bg)
draw.ellipse((x, y, x+h, y+h), fill=bg)
draw.rectangle((x+(h/2), y, x+w+(h/2), y+h), fill=bg, width=10)
# draw progress bar
progress = ((already_earned / to_reach ) * 100)
w *= progress
draw.ellipse((x+w, y, x+h+w, y+h),fill=fg)
draw.ellipse((x, y, x+h, y+h),fill=fg)
draw.rectangle((x+(h/2), y, x+w+(h/2), y+h),fill=fg, width=10)
return d
drawProgressBar(img, 10, 10, 100, 25, 0.5)
### From StackOverflow ###
img.save('leveling/infoimg2.png') # Save it and send it out
Second example:
async def rank(self, ctx, member: discord.Member):
member = ctx.author
data = await database.find_user(collection_user, ctx.guild.id, ctx.author.id)
already_earned = data["exp"]
to_reach = ((50 * (data['lvl'] ** 2)) + (50 * (data['lvl'] - 1)))
percentage = ((already_earned / to_reach) * 100) # Get the percentage
img = Image.open("leveling/rank.png")
draw = ImageDraw.Draw(img)
### From StackOverflow ###
color=(129, 66, 97)
x, y, diam = percentage, 8, 34
draw.ellipse([x,y,x+diam,y+diam], fill=color)
ImageDraw.floodfill(img, xy=(14,24), value=color, thresh=40)
### From StackOverflow ###
font = ImageFont.truetype("settings/myfont.otf", 35)
font1 = ImageFont.truetype("settings/myfont.otf", 24)
async with aiohttp.ClientSession() as session:
async with session.get(str(ctx.author.avatar)) as response:
image = await response.read()
icon = Image.open(BytesIO(image)).convert("RGBA")
img.paste(icon.resize((156, 156)), (50, 60))
# Draw some other text here, this works though
img.save('leveling/infoimg2.png') # Save the file and output it
Both results do not match the pictures shown in the answers and questions.
Is somebody able to tell me where I went wrong?
I also tried to increase x
in the second example or set img = Image.open("pic.png").format('RGB')
but nothing seems to work. The progress bar is either too long or too short.
I tried to achieve that my progress bar is restricted to some size, always matches 100% and that my defined progress
will adapt to it.