Reputation: 1
Screenshot:
Essentially I'm trying to make a folder with all the frames a video has, and editing it later in the code, however, I'm facing a problem of the file size of the img/frame being too high when saved which I have been trying to look into but I had no luck so far.
Any help will be appreciated!
VideoCap = cv2.VideoCapture("./Path/Path.mp4");
FolderPath = "./Path/Path";
if not os.path.exists(FolderPath):
print(f"{FolderPath} doesn't exists. Creating...");
os.makedirs(FolderPath);
Success, Frame = VideoCap.read();
Count = 0;
while Success:
cv2.imwrite(FolderPath+"/Frame%d.png" % Count, Frame); # The size is way too much! Literally 1/15th of the video just in a frame.
print("Frame %d has been written" % Count);
Count+=1;
Success, Frame = VideoCap.read();
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print(f"{FolderPath} already exists. Moving on...");
Upvotes: 0
Views: 312
Reputation: 700
Use compression parameters while writing image. Refer the documentation
cv2.imwrite(filename, img, [cv2.IMWRITE_PNG_COMPRESSION, compression_value])
#compression_value range 0-9
For PNG, it can be the compression level ( CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
Upvotes: 1