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))
#update_img=[]
writer=imageio.get_writer("smiling.gif", mode="I")
while cap.isOpened():
ret, frame = cap.read()
# if frame is read correctly ret is True
'''if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break'''
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)
tt=tt[:,1]
min_pos=min(tt)
max_pos=max(tt)
update_img=frame[0:1920,(min_pos-5):(max_pos+5)]
writer.append_data(update_img)
#out.write(update_img)
#imageio.imwrite('gif.gif', update_img)
cv.imshow('frame', update_img)
if cv.waitKey(1) == ord('q'):
break
#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)
writer.append_data(frame)'''
cap.release()
cv.destroyAllWindows()