2013-07-26 2 views
2

Предположим, у вас есть изображения i-1 в некоторой папке. Как я могу сделать этот код вынести их в некоторой сетке с числом столбцов int((i-1)**0.5) (как это сделало бы квадрат изображений)Как построить аранжировку .pngs с matplotlib

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) 

ответ

4

непроверенным, но вот общая идея.

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() 
+0

Большое спасибо за этот ответ, однако, когда я это я получаю: AttributeError: «модуль» объект имеет – Freeman

+0

Опечатка без атрибута «suplots» с моей стороны. Должно быть, «подзаголовки». –

+0

Ах, спасибо, просто проверяю и читаю. – Freeman

Смежные вопросы