I need to drop all the B-frames and P-frames, only keeping the I-frames to be saved as images.
You can instruct the decoder to skip non-keyframes using -skip_frame.
-skip_frame
ffmpeg -skip_frame nokey -i input.mp4 -vsync vfr img%04d.png