0

So I have this grey bar but I want to make a little part of it like this. But I don't want to download the blue bar, I just want to make it with pure code using Python

Takashi
  • 1
  • 3

1 Answers1

2

I am not sure how you are really supposed to code it, but I thought if I draw a cyan-coloured circle at the right end of the progress bar, and then flood-fill behind it from the left, it should work:

#!/usr/bin/env python3

from PIL import Image, ImageDraw

# Open template and get drawing context
im = Image.open('progress.png').convert('RGB')
draw = ImageDraw.Draw(im)

# Cyan-ish fill colour
color=(98,211,245)

# Draw circle at right end of progress bar
x, y, diam = 254, 8, 34
draw.ellipse([x,y,x+diam,y+diam], fill=color)

# Flood-fill from extreme left of progress bar area to behind circle
ImageDraw.floodfill(im, xy=(14,24), value=color, thresh=40)

# Save result
im.save('result.png')

enter image description here

Just so you understand what I am doing, I draw a circle where the picture below is marked up in red, and then floodfill starting where the picture is marked up in yellow:

enter image description here

So, if you want more progress shown, just increase x in the code - if you want less progress, decrease x.


As regards the quality of the rounded ends, you are bound to get jaggy edges if you start with a circle of radius 17 and them scale it up. Here's what ImageMagick does, on the left with anti-aliasing and on the right without:

enter image description here

Keywords: Python, image processing, PIL, Pillow, progress, bar, progress-bar, rounded ends.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hello. Thank you for your answer. But how do you exactly know where to put the circles? I cropped out the full image so I have to put it to a different location. – Takashi Apr 30 '20 at 14:34
  • Just load the image into Photoshop or GIMP or some image viewer and move the mouse to the yellow point and get its `x,y` coordinates and put them in the code. Then do the same for the top-left of the red rectangle. Set the diameter of the rectangle to height (i.e. diameter) of the circle you want and set `x` according to progress to be displayed. – Mark Setchell Apr 30 '20 at 14:54
  • Thank you! I've got another issue. How do I make the quality of the circle better? The outline's quality isn't as desired. Please help me. – Takashi Apr 30 '20 at 15:24
  • I can't see what quality you have got! If you started with a low resolution image you won't do very well. Try starting with a higher resolution image and scaling down rather than starting with a low resolution one and scaling up. – Mark Setchell Apr 30 '20 at 15:28
  • I think you didn't understand my issue. The quality of those ellipses aren't good in PIL for everyone. Just look closely and see the difference in endings of the progress. – Takashi Apr 30 '20 at 15:31
  • I have added an enlarged diagram of what happens when any program makes a small circle larger. Please show your image and I will try and help further. – Mark Setchell Apr 30 '20 at 15:47
  • Do you have Discord? – Takashi Apr 30 '20 at 15:52
  • An example or the code for what? I have given you the code I used in my answer. – Mark Setchell Apr 30 '20 at 16:06
  • An example of antialising images in PIL or Pillow – Takashi Apr 30 '20 at 16:09
  • Just make every thing 4 or 5 times bigger and then resize it down with `im = resize(im.size, Image.ANTIALIAS)` like this https://stackoverflow.com/a/22336005/2836621 – Mark Setchell Apr 30 '20 at 16:16
  • Give me an example please, I've tried what you said but it's not working. – Takashi Apr 30 '20 at 18:13