python - 删除使用 imageio 或 PIL 创建的 GIF 中的空间插值或压缩

我正在尝试使用 imageio 创建 GIF。这相当简单;但是,图像质量很差。它们似乎是插值的。请参见下图。

我正在使用下面显示的代码创建 GIF。我已经尝试跟踪源代码中图像的生成方式,问题似乎来自 PIL。

from PIL import Image
import imageio
import numpy as np
import matplotlib.pyplot as plt

outdir = r"where/you/want/it/to/go.gif"
frames = np.round(100+20*np.random.randn(10, 40, 40)).astype(np.uint8)

# Create single gif frame with PIL
im = Image.fromarray(frames[0])
im.save(outdir)

# Create gif with imageio
imageio.mimsave(outdir, frames)

我想要一个看起来像我用 matplotlib 生成的顶部图像(无插值)的 GIF。底部的图像是使用 imageio 生成的单帧。

最佳答案

我试图重现该问题,但据我所知,ImageIO 或 Pillow 内部均未发生插值:

from PIL import Image
import imageio as iio
import numpy as np
import io

for _ in range(100):
    expected_frames = np.round(100+20*np.random.randn(10, 40, 40)).astype(np.uint8)

    # test writing with pillow
    pil_buffer = io.BytesIO()
    im = Image.fromarray(expected_frames[0])
    gif_bytes = im.save(pil_buffer, format="GIF")
    actual_pil = iio.v3.imread(pil_buffer, mode="L")
    assert np.all(actual_pil == expected_frames[0])

    # test writing with ImageIO
    gif_bytes = iio.v3.imwrite("<bytes>", expected_frames, format="GIF") 
    actual_iio = iio.v3.imread(gif_bytes, index=None, mode="L")
    assert np.all(actual_iio == expected_frames)

print("No roundtrip modified any pixels.")

所以我认为要么

  • Pillow 或 ImageIO 中可能存在已修复的错误(已 3 年)
  • 您用来显示第二张图片的工具/应用程序会在您放大图片时进行插值(因为该图片是 200x200 像素而不是 40x40 像素)

https://stackoverflow.com/questions/49243732/

相关文章:

google-bigquery - bigquery 备份所有 View 定义

google-app-engine - 如何在 objectify 中通过嵌套 Refs 进行查询

python - 如何使用 R 按列将 .csv 拆分为多个 .csv?

json - 使用 REST API 的 POST 方法进行读取操作

python - 如何对 Pandas RE .str.extract() 使用 RE OR 操作数

c# - 如何在 OData v4 中获取枚举值而不是枚举名称?

android - 在 test.Jar 中启动 startActivity() ,Cordova

security - 自动关联社交登录有什么安全风险(Oauth2)

apache-flink - 在不重启作业的情况下在 Apache Flink 中动态添加模式

python - BeautifulSoup- find_all- 订单保存