【问题标题】:How to display multiple jpeg images in a table using python? [duplicate]如何使用 python 在表格中显示多个 jpeg 图像? [复制]
【发布时间】:2019-03-06 15:57:16
【问题描述】:

我的目录中有一些 jpeg。我想在窗口中以行和列的形式显示它们。例如,如果我有 10 张图片,我想将它们显示为 2 行 x 5 列的表格。
在 MATLAB 和 Octave 中有一个 subplot(m, n, k) 命令。我怎样才能在python中做类似的事情?

我尝试过使用 PIL.Image 和 show() 方法的枕头,但它非常有限,仅显示 1 张图像。

1- 如何在本机(不在浏览器中)执行此操作?
2- 如何使用 matplotlib 执行此操作?
3- 如何在使用 Jupyter 的浏览器中执行此操作?

【问题讨论】:

  • @DizietAsahi 认为他想要 matplotlib 中的相同功能。
  • @Andrei 你说得对,我没有仔细看我链接的帖子,我以为是 matplotlib 代码
  • @DizietAsahi matplotlib 也可以显示本地 jpeg 吗?
  • 这实际上是三个问题,每个问题都有自己的答案。 (1) 用 Pillow 连接图像。 (2) 在 matplotlib 中使用子图并显示图像 (3) 创建一个包含图像的 HTML 表格。这些是正交的解决方案,虽然我可能会帮助其中任何一个,但用代码为所有三个编写完整的答案超出了我目前的带宽。另外,这些问题中的每一个都可能已经在某个地方得到了回答。

标签: python image matplotlib ipython jupyter-notebook


【解决方案1】:
import matplotlib.pyplot as plt
from PIL import Image

fig,ax = plt.subplots(2,5)

filenames=['\path\to\img\img_{}.jpg'.format(i) for i in range(10)] #or glob or any other way to describe filenames
for i in range(10):
    with open(filenames[i],'rb') as f:
        image=Image.open(f)
        ax[i%2][i//2].imshow(image)
fig.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多