python - 如何使用 matplotlib 绘制 .png 数组

标签 python arrays image numpy matplotlib

假设您在某个文件夹中有一些 i-1 图像。我怎样才能让这段代码将它们呈现在一些具有列数的网格中 int((i-1)**0.5) (因为这会形成一个正方形的图像)

import matplotlib.pyplot as plt
import matplotlib.image as mplimg
import pylab
import numpy as np

for j in range(i):
   image=mplimg.imread("c:\\users\\laurence\\dropbox\\ggl\\images\\"+str(j)+".png")
   arr=np.asarray(image)

最佳答案

未经测试,但这是总体思路。

import matplotlib.pyplot as plt
import glob
import numpy as np

# glob won't preserve the order that the files are in (if you need that, you can
# simply do what you were already doing. Globbing is simpler, though.
filenames = glob.glob('c:/path/to/your/photos/*.png')
# Forward slashes work for pathnames on windows, too (at least in python)

# Let's not assume that there's an exact square number of images
nrows = np.ceil(np.sqrt(len(filenames))).astype(int)
ncols = len(filenames) // nrows

# Subplots returns a figure and a _2d array_ of axes in a grid.
fig, axes = plt.subplots(nrows, ncols)

# Note that we're iterating over ``axes.flat``, not just axes (which is 2d)
for filename, ax in zip(filenames, axes.flat):
    data = plt.imread(filename)
    ax.imshow(data)

    # You might want to hide the labels, border, etc
    ax.axis('off')

# Not necessary, but this will give you more evenly located subplots
fig.tight_layout() 
plt.show()

关于python - 如何使用 matplotlib 绘制 .png 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882122/

相关文章:

python - 设置 matplotlib 3D 图的刻度颜色

javascript - 通过 javascript 从具有值的数组中创建 arraylist

python - 3D 语义分割任务的 Keras 预处理

objective-c - UIScrollview 在缩小时更改 UIButton 的图像质量

python -/invoice/Flowable <PmlTable@0x1D09C899130 7 rows x 5 cols(tallest row 841)> with cell(0,0) 的布局错误

python - 在 Python 中写入(而不是)全局变量

python - Pandas DataFrame - 将系列字符串拆分为多列

arrays - 加载字段 1 并在 Perl 中的 END{} 等效 awk 处打印

python - python中三维数组的输入

javascript - 是否可以使用任何网络语言在图像上放置纹理蒙版?