我目前正在尝试根据计算出的百分比绘制进度条。
但是,我无法以正确的格式显示它。
我参考了这个网站上的另一个答案(如何制作进度条并将其放在图像上?和是否可能使用PIL或Pillow添加蓝色条?)
但是,要么进度条太长而且width
限制不起作用,要么进度条没有显示进度。
例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
第二个例子:
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
这是它的样子:
![enter image description here](https://istack.dev59.com/mlJ1X.webp)
两个结果都与答案和问题中展示的图片不符。
有人能告诉我哪里出错了吗?
我还尝试增加第二个示例中的 x
或设置 img = Image.open("pic.png").format('RGB')
,但似乎没有任何作用。进度条要么太长,要么太短。
我试图实现我的进度条被限制在某些大小范围内,始终匹配100%,并且我的定义的 progress
将会适应它。