<p>JPEG文件(压缩图像)以始终包含标记代码十六进制值FF D8 FF的图像标记开始。它并没有嵌入文件的长度,所以我们需要找到JPEG拖车,它是FF D9</p>
<p>请使用位于<a href="https://www.file-recovery.com/jpg-signature-format.htm" rel="nofollow noreferrer">this page</a>的链接查看文档</p>
<p>例如,使用十六进制查看器(例如<a href="https://mh-nexus.de/en/hxd/" rel="nofollow noreferrer">Hex Viewer</a>)打开jpeg图像时,您应该会看到如下内容:</p>
<p><a href="https://i.stack.imgur.com/513UG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/513UG.png" alt="enter image description here"/></a></p>
<p><strong>解决方案</strong>:换句话说,在将文件保存为JPEG之前,尝试将文件头添加到文件中,您应该可以解决问题</p>
<p>可以在<a href="https://imageio.readthedocs.io/en/stable/userapi.html" rel="nofollow noreferrer">here</a>找到包含API文档的页面。在文档之后,您应该找到正确的指令,该指令使您指定保存格式(正如@Meto在回答中指出的)</p>
<p>结论:解决方案只是指定在硬盘中物理写入图像时的格式:</p>
<pre><code>imageio.imwrite(uri, im, format=None, **kwargs)
</code></pre>
<p>在你的例子中<code>format=jpg</code></p>
<p>而且</p>
<pre><code> imageio.show_formats()
</code></pre>
<p>显示格式良好的可用格式列表</p>
<p>最后,试着替换</p>
<pre><code>imageio.imwrite(output_path, image)
</code></pre>
<p>与</p>
<pre><code>imageio.imwrite(output_path, image, format ='jpg' )
</code></pre>
<p><strong>请注意</strong>每个答案的解决方案总是相同的。我刚刚添加了指定格式时发生的事情(即,只写正确的标题)</p>