开发者社区> 问答> 正文

有没有一种更有效的方法在枕头上创建进度条?

我正在为一个用于验证保险的python脚本创建一个简单的图像输出。 我开始用pillow做实验,只是写了一个函数来做进度条,我对python和编程也很陌生,英语不是我的第一语言。 我的问题是,是否有更好的(当然有)和更有效的方法来处理元组?,还在计算进度条的中间放文本吗?。 我知道代码是一个混乱的时候,我只是测试功能。其他任何建议都很好。谢谢!

from PIL import Image, ImageDraw, ImageFont

img = Image.new('RGB', (500, 450), color = 'white')

fnt = ImageFont.truetype('Arial.ttf', 15)

d = ImageDraw.Draw(img)

d.text((10,10), "Prophylaxis", font=fnt, fill=('black'))

def draw_shape_text(xyshape, text, perc, firstc="green", secondc="red", textc="white"):
  """Draws a progress bar with text on the inside that changes depending on the percentage
        Parameters
        ----------
        xyshape: list
            Coordinates of the progress bar [(x1,y1),(x2,y2)]
        text: str
            Text to be shown inside of the progress bar.
        perc: int
            Percentage of the progress bar.
        firstc: str
            First color of the progress bar. Default: "Green".
        secondc: str
            Second color of the progress bar. Default: "Red".
        textc: str
          Color of the text. Default: "White". 

        Returns
        -------
        None"""

  #Getting the coordinates out of the tuple
  (x1, y1), (x2, y2) = xyshape

  #calculating the total progress bar length and middle
  shape_length = x2 - x1
  middle = shape_length/2


  #change offset to move the text.
  offset = 32.5
  #position of the text inside the progress bar.
  xytext = (x1 + middle - offset, y1 + 2)

  #converting the perc integer value to a percentage float value.
  percentage = perc/100

  #calculating the inner shape of the progress bar to work as the progress shown.
  xyshape_percentage = [xyshape[0], (shape_length * percentage + x1, y2)]

  #drawing the first outside shape.
  d.rectangle(xyshape, fill=secondc, outline=None)
  #drawing the second inner shape.
  d.rectangle(xyshape_percentage, fill=firstc, outline=None)

  #putting the text after drawing the two shapes.
  d.text(xytext, text, font=fnt, fill=textc)

percentage=int(input("Insert percentage: "))

if percentage < 100:
  draw_shape_text([(300, 10), (457, 30)], "Not Eligible", percentage)
else:
  draw_shape_text([(300, 10), (475, 30)], "Eligible", percentage)

img.save('pil_text.png')

问题来源StackOverflow 地址:/questions/59379161/is-there-a-more-efficient-way-to-create-a-progress-bar-in-pillow

展开
收起
kun坤 2019-12-30 09:37:36 436 0
1 条回答
写回答
取消 提交回答
  • 我们想象中的进度条,就是在一行的,然后会动态的刷新。例如一共有 20 个格,一开始都是 "-",当进度多完成了 5% 我们就将一个 “-” 转变成 "*",最好的在这一行的结尾还有一个数字,可以以百分比的形式进一步展示。实现这个功能,查了一下发现需要两个函数,sys.stdout.write 和 sys.stdout.flush。

        sys.stdout.write 实际上是我们常用的 print 背后所调用的内容,用来控制输出。通过几段简单的代码和结果可以帮助我们理解这个函数以及 sts.stdout.flush() 二者的效果,代码如下:
    

    import sys, time for i in range(5): sys.stdout.write(str(i)) time.sleep(1)

    01234

    这里的输出将会是迟滞5秒之后一次性打印出 01234。下一个例子是加上 \n 即换行符:

    import sys, time for i in range(5): sys.stdout.write(str(i) + "\n") time.sleep(1)

    0

    1

    2

    3

    4

    加上了换行符之后结果不同了,现在每隔一秒就会在一行中打印出一个数字。我们可以看出,sys.stdout.write 相当于有一个光标,我们在这个光标所在的位置开始写入内容;但是它似乎有一个缓冲的机制,并不会每当一个 sys.stdout.write 出现就马上执行,而是需要等缓冲区满了(具体的含义不明),然后才会一并输出。所以才需要用到 sys.stdout.flush 这个函数,它的效果就是不再需要等缓存区满了才输出,而是立刻打印出现在在缓存区中的内容。

    import sys, time for i in range(5): sys.stdout.write(str(i)) sys.stdout.flush() time.sleep(1)

    01234

    以上的效果就是每隔一秒打印出一个数字。

        但是如果我们想要有一个进度条的效果,根据我们一开始的设想,这种在最后添加内容的 write 方法不是很合适,真正需要的是可以把我们之前打印的东西重置的一个操作。这里就用到一个转义字符 \r,它的作用是使光标回到该行的开头,那么我们下次输出的时候就会有一个覆盖的效果,看起来就像是刷新了。
    

    import sys, time for i in range(5): s = str(i)*(5 - i) sys.stdout.write(s + "\r") sys.stdout.flush() time.sleep(0.5) 这段小代码的效果就是一开始 00000 然后 11110、22210、33210,最后43210,可以看出的确不会把原先的数据除去而只是覆盖。所以我们只要保持相同的长度,然后使用这个转义字符,就可以刷新我们的进度条啦。

         我自己写的简单的进度条函数如下。max_bar 就是一条进度条一共有 20 格,两个形式参数 tot 和 pre 表示一共要完成的任务数和已经完成的任务数,通过简单的计算就可以确定我们要显示多少条已完成的条。后面有一个判断,是因为在我们的进度条执行完毕之后,光标的位置就要换行而不能回到该行的开头,否则后续其他的打印操作将会覆盖我们的进度条,就显得很不公整。
    

    def progressbar(tot, pre): ''' max_bar means the total number of tasks. i means the number of finished tasks. ''' max_bar = 20 finish = int(pre*max_bar/tot) unfinish = (max_bar - finish) bar = "[{}{}]".format(finish * "-", unfinish * " ") percent = str(int(pre/tot * 100)) + "%" if pre < tot: sys.stdout.write(bar + percent + "\r") else: sys.stdout.write(bar + percent + "\n") sys.stdout.flush()

    2021-02-16 01:39:38
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
3D动画的菜谱式灯光与云渲染 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载