Reputation: 167
I have a JPEG image I
and a transform function F
which has some state S
. Every N
minutes function's state changes and after that F
is applied to my image I
. So for the K'th
change we have output image I_k
which is saved to some directory FRAMES_DIR
.
What I want to do is to have an animated WEBP with all generated images so far. Let's call it W_k = WEBP(I_1, ..., I_k)
. Also it should be done in memory without any saves on disk.
My current approach is using Python and PIL library and looks like that:
webp_frames = []
def get_webp():
global webp_frames
nb_frames_processed = len(webp_frames)
for frame_idx, frame_path in enumerate(sorted(os.listdir(FRAMES_DIR))):
if frame_idx < nb_frames_processed:
continue
frame = Image.open(os.path.join(FRAMES_DIR, frame_path))
webp_frames.append(frame)
webp = io.BytesIO()
webp_frames[0].save(webp, format='WEBP', save_all=True, loop=0, append_images=webp_frames[1:], duration=400)
return webp
The problem is that after each call of get_webp
webp is generated from scratch. It is very slow and has O(K)
complexity. I want to find a way to make it in O(1)
time by appending a new frame to the end of current WEBP file. Is there any pretty way to do it in Python?
Upvotes: 0
Views: 61