Repeating animation of *.gif file in opencv python and Writing *.gif file

I am trying to repeat animation of *.gif file in opencv python code. But Animation stops at the end of frame.

Next i wanted to write *.gif file. But opencv does not have fourcc codec. Then i have found one python package called imageio. Even through *.gif file can be written through imageio but color is completely different from original raw *.gif file.

Here is code;

import numpy
import cv2 as cv
import imageio
cap = cv.VideoCapture('movie_000.gif')
# Define the codec and create VideoWriter object
#fourcc = cv.VideoWriter_fourcc('G','I','F')
#out = cv.VideoWriter('output.gif', fourcc, 10.0, (1920, 1080))
writer=imageio.get_writer("smiling.gif", mode="I")
while cap.isOpened():
    ret, frame =
    # if frame is read correctly ret is True
    '''if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    ret,thresh1 = cv.threshold(gray,242,255,cv.THRESH_BINARY)
    #gray = cv.cvtColor(frame)
    tt=numpy.argwhere(thresh1 == 0)
    #imageio.imwrite('gif.gif', update_img) 
    cv.imshow('frame', update_img)
    if cv.waitKey(1) == ord('q'):
#imageio.imwrite('astronaut-gray.gif', update_img)
'''with imageio.get_writer("smiling.gif", mode="I") as writer:
    for idx, frame in enumerate(frames):
        print("Adding frame to GIF file: ", idx + 1)

entirely off-topic, i’d say. this is a computer-vision library, not a video-editing one.

and VideoWriter probably won’t write animated gif’s
(still a license problem)

also, opencv uses bgr pixel order, imageio rgb.
if you blindly mix different image processing libs, you run into problems like this