0

I have an mp4 video file with these characteristics:

video description according to VLC
-Encoder: Lavf58.29.100

I want to extract each frame as an image, with the following requirements:

  • one image file is one frame (i.e., account for variable framerates to work in all cases),

  • the image file has to be lossless TIFF.

I use Linux and would prefer Python to do this, but I can also use FFmpeg.

However, I have seen some other posts here, but they are either not-replied to or just need JPG format. I have read the imwrite documentation (OpenCV) and saw that it is actually possible to export as TIFF (even with different bit-depths), but I can't see an example of using that. I also do not know how to confirm if the exported TIFF is actually without compression.

The reason I need each frame as a lossless image file is that I need the images for further analysis in research, and I don't want to lose any information at this stage (the sequence of images is the starting 'raw material' of my analysis pipeline).

2
  • You don't need tiff. png or jpg at 100% will do just as well. It's not compression you need to avoid, it's lossy compression.
    – Tetsujin
    Apr 27, 2023 at 16:11
  • Thank you. Then I will try with cv2.imwrite(name, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])
    – terauser
    Apr 27, 2023 at 17:42

2 Answers 2

0

After research and help from here and other places, I found 3 ways to do it.

Pillow (Python)

  • Command: img.save(output_filename,format='TIFF',compression='tiff_deflate',tiffinfo={})
  • Result: Generates 2246 TIFF files (variable sizes, between 761 and 566 KiB)

OpenCV (Python)

  • Command: cv2.imwrite(name, frame, [cv2.IMWRITE_TIFF_COMPRESSION, 1])
  • Result: Generates 2246 TIFF files (6.4 MiB each one)

ffmpeg (tested on Linux)


Edit 1: The differences between file sizes in each option (e.g. 600 KiB vs 6.4 MiB vs 2.8 MiB) seem to be related to the compression used by different algorithms. I used exiftool and found that in deed the compression is different: uncompressed (Python/OpenCV), PackBits (ffmpeg) and Adobe Deflate (Python/Pillow).

Edit 2: after further inspection and comparison, I observed that Python/Pillow (which uses Adobe Deflate method) also significantly changed the colors in the picture! I am unsure if this is due to the compression method or is something else done by Pillow.

2
  • Try to get details about the TIFF files via exiftool. TIFFs can have different bit depth, different number of channels, different compression methods. You can even have JPEG inside a TIFF.
    – U. Windl
    Apr 28, 2023 at 18:43
  • Thank you for a great suggestion! I used exiftool and found that in deed the compression is different: uncompressed (Python/OpenCV), PackBits (ffmpeg) and Adobe Deflate (Python/Pillow).
    – terauser
    Apr 28, 2023 at 21:23
0

This ffmpeg command should do the trick for you:

ffmpeg -i in.mp4 -vf "select=eq(n\,0),setpts=N/(FRAME_RATE*TB)" output_%08d.tif

Note: Your input is allready encoded in a lossy encoding, so you will have artifacts from the h264 codec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.