繁体   English   中英

你如何制作进度条并将其放在图像上?

[英]How do you make a progress bar and put it on an image?

我正在尝试将进度条放在图像上。

我有一个 discord 机器人,其等级系统如下: 在此处输入图像描述

现在我想要做的是将水平作为进度条,满分 20。一旦他们达到 20,新目标将是 40。

我试过寻找教程和阅读网站,但我找不到任何关于将进度条放在图像上的信息。

我的最终目标是这样的(我从 MEE6 得到的): 在此处输入图像描述

有没有办法使用 Python 和枕头或其他模块来做到这一点?

您可以通过以下方式绘制自己的进度:

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

def drawProgressBar(d, x, y, w, h, progress, bg="black", fg="red"):
    # draw background
    d.ellipse((x+w, y, x+h+w, y+h), fill=bg)
    d.ellipse((x, y, x+h, y+h), fill=bg)
    d.rectangle((x+(h/2), y, x+w+(h/2), y+h), fill=bg)

    # draw progress bar
    w *= progress
    d.ellipse((x+w, y, x+h+w, y+h),fill=fg)
    d.ellipse((x, y, x+h, y+h),fill=fg)
    d.rectangle((x+(h/2), y, x+w+(h/2), y+h),fill=fg)

    return d

# create image or load your existing image with out=Image.open(path)
out = Image.new("RGB", (150, 100), (255, 255, 255))
d = ImageDraw.Draw(out)

# draw the progress bar to given location, width, progress and color
d = drawProgressBar(d, 10, 10, 100, 25, 0.5)
out.save("output.jpg")

这将创建如下内容:

在此处输入图像描述

有关绘图的更多详细信息,请查看文档,您可以在其中找到一些示例,例如添加文本等...

使用来自@Bastian 的示例,我对其进行了一些调整,使其在接近 0 的小数字时更加准确:

## if progress < .5 (half)
#   put the "progress" in the background with 2 objects (begin ellipse and rectangle)
#   put the "unfinished" part in the foreground with 3 objects (begin ellipse, rectangle, end ellipse)
#     make the unfinished begin ellipse get flatter and flatter as it approaches 50%
## if progress >= .5 (half)
#   put the "progress" in the foreground with 3 objects (begin ellipse, rectangle, end ellipse)
#   put the "unfinished" part in the background with 2 objects (rectangle, end ellipse)
#     make the progress end ellipse start flat and get rounder as it approaches 100%

if progress < 0.5:
    # draw progress bar
    d.ellipse((x, y, x+h, y+h),fill=fg)
    d.rectangle((x+(h/2), y, x+w+(h/2), y+h),fill=fg)
    # d.ellipse((x+w, y, x+h+w, y+h),fill=fg)

    # draw background (unfinished portion)
    d.ellipse((x+w, y, x+w+(h*(1-(2*progress))), y+h), fill=bg)
    d.rectangle((x+w+(h/2)-(h*progress), y, e+(h/2), y+h), fill=bg)
    d.ellipse((e, y, e+h, y+h), fill=bg)

if progress >= 0.5:
    # draw background (unfinished portion)
    # d.ellipse((x, y, x+h, y+h), fill=bg)
    d.rectangle((x+w, y, e+(h/2), y+h), fill=bg)
    d.ellipse((e, y, e+h, y+h), fill=bg)

    # draw progress bar
    d.ellipse((x, y, x+h, y+h),fill=fg)
    d.rectangle((x+(h/2), y, x+w+(h*(progress-0.5)), y+h),fill=fg)
    d.ellipse((x+w, y, x+w+h*2*(progress-0.5), y+h),fill=fg)

调整进度条的演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM