Skip to content

Instantly share code, notes, and snippets.

@jeanpat
Created February 21, 2014 15:20
Show Gist options
  • Save jeanpat/9136074 to your computer and use it in GitHub Desktop.
Save jeanpat/9136074 to your computer and use it in GitHub Desktop.
Given a list of monochrome images or multispectral images (example rgb, or also a z-stack of images) build a list of images of the same size by adding borders.
def ResizeImages(ImList):
'''Find the largest width and height of images belonging to a list.
Return a list of images of same width/height
'''
maxwidth=0
maxheight=0
if len(np.shape(ImList[0]))==3:
components = np.shape(ImList[0])[2]
imtype = ImList[0].dtype
for i in range(len(ImList)):
width=np.shape(ImList[i])[1]#width=column
height=np.shape(ImList[i])[0]#height=line
#print "width:height",width,":",height
if width>maxwidth:maxwidth=width
if height>maxheight:maxheight=height
#print "maxwidth:maxheight",maxwidth,":",maxheight
NewList=[]
for i in range(0,len(ImList)):
width=np.shape(ImList[i])[1]
height=np.shape(ImList[i])[0]
diffw=maxwidth-width
startw=round(diffw/2)
diffh=maxheight-height
starth=int(round(diffh/2))
startw=int(round(diffw/2))
if len(np.shape(ImList[0]))==3:
newIm=np.zeros((maxheight,maxwidth,components), dtype=imtype)
newIm[starth:starth+height,startw:startw+width,:]=ImList[i][:,:,:]
NewList.append(newIm)
if len(np.shape(ImList[0]))==2:
newIm=np.zeros((maxheight,maxwidth), dtype=imtype)
newIm[starth:starth+height,startw:startw+width]=ImList[i][:,:]
NewList.append(newIm)
return NewList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment