[英]How do you make a progress bar and put it on an image?
您可以通过以下方式绘制自己的进度:
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.