I'm trying to overlay a dot on a radar GIF from my local news station, which updates every 5 minutes. The GIF is 1920x1080 and has 305 frames. Right now, using Pillow, it takes about 2 minutes to process the GIF to add the dot at my coordinates. My goal is to reduce the processing time to about 5 seconds if possible. Here's my current script for adding the dot:
```python
from PIL import Image, ImageDraw, ImageSequence
def overlayDot(input, output, dotPosition=(50, 50), dotRadius=5, dotColor="blue"):
doppler = Image.open(input)
frames = []
for frame in ImageSequence.Iterator(doppler):
frame = frame.copy()
draw = ImageDraw.Draw(frame)
x1 = dotPosition[0] - dotRadius
y1 = dotPosition[1] - dotRadius
x2 = dotPosition[0] + dotRadius
y2 = dotPosition[1] + dotRadius
draw.ellipse((x1, y1, x2, y2), fill=dotColor)
frames.append(frame)
if frames:
frames[0].save(
output,
save_all=True,
append_images=frames[1:],
duration=doppler.info.get("duration", 100),
loop=doppler.info.get("loop", 0),
)
else:
print("No frames found in the input GIF.")
overlayDot(r"C:Usersalanator222DocumentsPython ScriptsDoppler Radarradar.gif", r"C:Usersalanator222DocumentsPython ScriptsDoppler Radaroutput.gif", (500,500), 50, "blue")
```
Can anyone suggest how to make this faster?
4 Answers
If you could share a sample GIF or link, I could run a few tests and see if I can find a faster approach for you.
Have you considered not processing the GIF at all? If you're displaying it on a different website, maybe you could overlay your dot on a separate layer in front of the GIF. This would save you the hassle of re-editing the GIF for every update.
You might speed things up a bit by pre-drawing the dot and then pasting it into each frame instead of drawing it in the loop. Just a thought! But definitely benchmark where the time is going first before making changes; random tweaks might not help much without knowing what's slowing you down.
That could work! Thanks for the suggestion, I'll give it a shot.
Make sure to profile your code to identify the bottlenecks. Many IDEs like PyCharm have built-in tools for profiling, which can help you see where the majority of the processing time is spent.
I'll definitely try that. Thanks!
Ideally, yes! I could do that. My goal is to have this GIF show in a tasker scene on my phone, allowing me to zoom in, which is why I wanted to edit the GIF directly.