<hr />
<p>After research and help from here and other places, I found 3 ways to do it.</p>
<p><strong>Pillow (Python)</strong><br></p>
<ul>
<li>Command: <code>img.save(output_filename,format='TIFF',compression='tiff_deflate',tiffinfo={})</code><br></li>
<li>Result: Generates 2246
TIFF files (variable sizes, between 761 and 566 KiB)</li>
</ul>
<p><strong>OpenCV (Python)</strong><br></p>
<ul>
<li>Command: <code>cv2.imwrite(name, frame, [cv2.IMWRITE_TIFF_COMPRESSION, 1])</code><br></li>
<li>Result: Generates 2246 TIFF files (6.4 MiB each one)</li>
</ul>
<p><strong>ffmpeg (tested on Linux)</strong><br></p>
<ul>
<li>Command: (<a href="https://superuser.com/questions/1781258/avoid-duplicate-frames-in-ffmpeg">see different ways</a>)<br></li>
<li>Result: Generates 2246 or 2247 TIFF files, depending on the exact command (<a href="https://superuser.com/questions/1781258/avoid-duplicate-frames-in-ffmpeg">see different ways</a>). Each file is 2.8 MiB<br></li>
</ul>
<hr />
<p><strong>Edit 1</strong>: 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 <a href="https://exiftool.org/" rel="nofollow noreferrer">exiftool</a> and found that in deed the compression is different: <em>uncompressed</em> (Python/OpenCV), <em>PackBits</em> (ffmpeg) and <em>Adobe Deflate</em> (Python/Pillow).</p>
<p><strong>Edit 2</strong>: after further inspection and comparison, I observed that Python/Pillow (which uses <em>Adobe Deflate</em> 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.</p>

CC BY-SA 4.0
2023-04-28T18:43:21.030Z
Try to get details about the TIFF files via [`exiftool`](https://exiftool.org/). TIFFs can have different bit depth, different number of channels, different compression methods. You can even have JPEG inside a TIFF.
U. Windl
CC BY-SA 4.0
2023-04-28T21:23:09.473Z
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
CC BY-SA 4.0
2023-04-28T10:51:00.780Z
2023-05-02T09:42:31.897Z
2023-05-02T09:42:31.897Z
terauser
<p>This ffmpeg command should do the trick for you:</p>
<pre class="lang-bash prettyprint-override"><code>ffmpeg -i in.mp4 -vf &quot;select=eq(n\,0),setpts=N/(FRAME_RATE*TB)&quot; output_%08d.tif
</code></pre>
<p>Note: Your input is allready encoded in a lossy encoding, so you will have artifacts from the h264 codec.</p>

CC BY-SA 4.0
2023-09-30T07:23:38.587Z
2023-09-30T07:23:38.587Z
Euklios
<p>I have an mp4 video file with these characteristics:</p>
<p><img src="https://i.sstatic.net/4cQyI.png" alt="video description according to VLC" /><br>
-Encoder: Lavf58.29.100</p>
<p>I want to extract each frame as an image, with the following requirements:</p>
<ul>
<li><p>one image file is one frame (i.e., account for variable framerates to work in all cases),</p>
</li>
<li><p>the image file has to be lossless TIFF.</p>
</li>
</ul>
<p>I use Linux and would prefer Python to do this, but I can also use FFmpeg.</p>
<p>However, I have seen some other posts here, but they are either not-replied to or just need JPG format. I have read the <a href="https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html" rel="nofollow noreferrer">imwrite</a> 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.</p>
<p>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).</p>

CC BY-SA 4.0
2023-04-27T16:11:06.200Z
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
CC BY-SA 4.0
2023-04-27T17:42:55.987Z
Thank you. Then I will try with `cv2.imwrite(name, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])`
terauser
<ffmpeg><lossless>
lossless frame extraction from video
terauser
# lossless frame extraction from video

(Asked by: username_0 on 2023-04-27)

I have an mp4 video file with these characteristics:

![video description according to VLC](https://i.sstatic.net/4cQyI.png)  
-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](https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html) 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).

**Tags:** ffmpeg, lossless

> **Comments:**
> **username_1**: 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.  
> **username_0**: Thank you. Then I will try with `cv2.imwrite(name, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])`  

---

## 2 answers

### Answer by username_0 on 2023-04-28. Score: 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)**

* Command: ([see different ways](https://superuser.com/questions/1781258/avoid-duplicate-frames-in-ffmpeg))
* Result: Generates 2246 or 2247 TIFF files, depending on the exact command ([see different ways](https://superuser.com/questions/1781258/avoid-duplicate-frames-in-ffmpeg)). Each file is 2.8 MiB

---

**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](https://exiftool.org/) 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.

> **Comments:**
> **username_2**: Try to get details about the TIFF files via [`exiftool`](https://exiftool.org/). TIFFs can have different bit depth, different number of channels, different compression methods. You can even have JPEG inside a TIFF.  
> **username_0**: 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).  

---

### Answer by username_3 on 2023-09-30. Score: 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.


---

