More idiomatic way to display images in a grid with numpy

后端 未结 3 1468
青春惊慌失措
青春惊慌失措 2020-12-31 09:40

Is there a more idiomatic way to display a grid of images as in the below example?

import numpy as np

def gallery(array, ncols=3):
    nrows = np.math.ceil(         


        
3条回答
  •  独厮守ぢ
    2020-12-31 10:10

    Another way is to use view_as_blocks . Then you avoid to swap axes by hand :

    from skimage.util import view_as_blocks
    import numpy as np
    
    def refactor(im_in,ncols=3):
        n,h,w,c = im_in.shape
        dn = (-n)%ncols # trailing images
        im_out = (np.empty((n+dn)*h*w*c,im_in.dtype)
               .reshape(-1,w*ncols,c))
        view=view_as_blocks(im_out,(h,w,c))
        for k,im in enumerate( list(im_in) + dn*[0] ):
            view[k//ncols,k%ncols,0] = im 
        return im_out
    

提交回复
热议问题