[docs]defplot_gallery_images(imgs,texts=None,width=4,return_figure=False,ax=None,folder_image=None,**figure):""" Plots a gallery of images using :epkg:`matplotlib`. :param imgs: list of images (filename, urls or :epkg:`Pillow` objects), :param texts: text to display (if None, print ``'img % i'``) :param width: number of images on the same line (unused if *imgs* is a matrix) :param figure: additional parameters when the figure is created :param return_figure: return the figure as well as the axes :param ax: None or existing axes, it should have the sam shape of *imgs* :param folder_image: image paths may be relative to some folder, in that case, they should be relative to this folder :return: axes or (figure, axes) if *return_figure* is True .. image:: gal.jpg """if"plt"notinsys.modules:importmatplotlib.pyplotaspltifhasattr(imgs,"shape")andlen(imgs.shape)==2:height,width=imgs.shapeassert(axisNoneorax.shape==imgs.shape),f"ax.shape {ax.shape} != imgs.shape {imgs.shape}"imgs=imgs.ravel()iftextsisnotNone:texts=texts.ravel()else:height=len(imgs)//widthiflen(imgs)%width!=0:height+=1ifaxisNone:if"figsize"notinfigure:figure["figsize"]=(12,height*3)fig,ax=plt.subplots(height,width,**figure)elifreturn_figure:raiseValueError("ax is specified and return_figure is True")fori,imginenumerate(imgs):ifimgisNone:continuey,x=i//width,i%widthifheight==1:ind=xelifwidth==1:ind=yelse:ind=y,xifisinstance(img,str):if"//"inimg:# urlwithurllib.request.urlopen(img)asresponse:content=response.read()try:im=Image.open(io.BytesIO(content))exceptOSErrorase:raiseRuntimeError(f"Unable to read image '{img}'.")fromeelse:# local fileiffolder_imageisnotNone:im=Image.open(os.path.join(folder_image,img))else:im=Image.open(img)else:im=imgifhasattr(im,"size"):ax[ind].imshow(im)iftextsisNone:t="img %d"%ielse:t=texts[i]ax[ind].text(0,0,t)ax[ind].axis("off")foriinrange(len(imgs),width*height):y,x=i//width,i%widthifheight==1:ind=xelse:ind=y,xax[ind].axis("off")ifreturn_figure:returnfig,axreturnax