def draw_grid(images, rows=None, cols=None):
nb_images = len(images)<EOL>do_assert(nb_images > <NUM_LIT:0>)<EOL>if is_np_array(images):<EOL><INDENT>do_assert(images.ndim == <NUM_LIT:4>)<EOL><DEDENT>else:<EOL><INDENT>do_assert(is_iterable(images) and is_np_array(images[<NUM_LIT:0>]) and images[<NUM_LIT:0>].ndim == <NUM_LIT:3>)<EOL>dts = [image.dtype.name for image in images]<EOL>nb_dtypes = len(set(dts))<EOL>do_assert(nb_dtypes == <NUM_LIT:1>, ("<STR_LIT>"<EOL>+ "<STR_LIT>") % (nb_dtypes, "<STR_LIT:U+002CU+0020>".join(dts)))<EOL><DEDENT>cell_height = max([image.shape[<NUM_LIT:0>] for image in images])<EOL>cell_width = max([image.shape[<NUM_LIT:1>] for image in images])<EOL>channels = set([image.shape[<NUM_LIT:2>] for image in images])<EOL>do_assert(<EOL>len(channels) == <NUM_LIT:1>,<EOL>"<STR_LIT>"<EOL>+ "<STR_LIT>" % (str(channels), len(channels))<EOL>)<EOL>nb_channels = list(channels)[<NUM_LIT:0>]<EOL>if rows is None and cols is None:<EOL><INDENT>rows = cols = int(math.ceil(math.sqrt(nb_images)))<EOL><DEDENT>elif rows is not None:<EOL><INDENT>cols = int(math.ceil(nb_images / rows))<EOL><DEDENT>elif cols is not None:<EOL><INDENT>rows = int(math.ceil(nb_images / cols))<EOL><DEDENT>do_assert(rows * cols >= nb_images)<EOL>width = cell_width * cols<EOL>height = cell_height * rows<EOL>dt = images.dtype if is_np_array(images) else images[<NUM_LIT:0>].dtype<EOL>grid = np.zeros((height, width, nb_channels), dtype=dt)<EOL>cell_idx = <NUM_LIT:0><EOL>for row_idx in sm.xrange(rows):<EOL><INDENT>for col_idx in sm.xrange(cols):<EOL><INDENT>if cell_idx < nb_images:<EOL><INDENT>image = images[cell_idx]<EOL>cell_y1 = cell_height * row_idx<EOL>cell_y2 = cell_y1 + image.shape[<NUM_LIT:0>]<EOL>cell_x1 = cell_width * col_idx<EOL>cell_x2 = cell_x1 + image.shape[<NUM_LIT:1>]<EOL>grid[cell_y1:cell_y2, cell_x1:cell_x2, :] = image<EOL><DEDENT>cell_idx += <NUM_LIT:1><EOL><DEDENT><DEDENT>return grid<EOL>
Converts multiple input images into a single image showing them in a grid.

dtype support::

    * ``uint8``: yes; fully tested
    * ``uint16``: yes; fully tested
    * ``uint32``: yes; fully tested
    * ``uint64``: yes; fully tested
    * ``int8``: yes; fully tested
    * ``int16``: yes; fully tested
    * ``int32``: yes; fully tested
    * ``int64``: yes; fully tested
    * ``float16``: yes; fully tested
    * ``float32``: yes; fully tested
    * ``float64``: yes; fully tested
    * ``float128``: yes; fully tested
    * ``bool``: yes; fully tested

Parameters
----------
images : (N,H,W,3) ndarray or iterable of (H,W,3) array
    The input images to convert to a grid.

rows : None or int, optional
    The number of rows to show in the grid.
    If None, it will be automatically derived.

cols : None or int, optional
    The number of cols to show in the grid.
    If None, it will be automatically derived.

Returns
-------
grid : (H',W',3) ndarray
    Image of the generated grid.