code: def rebin(self, factor):
          
          
          if self.pilimage != None:
              raise RuntimeError, "Cannot rebin anymore, PIL image already exists !"
          
          if type(factor) != type(0):
              raise RuntimeError, "Rebin factor must be an integer !"
          
          if factor < 1:
              return
  
          origshape = np.asarray(self.numpyarray.shape)
          neededshape = origshape - (origshape % factor)
          if not (origshape == neededshape).all():
              if self.verbose :
                  print "Rebinning %ix%i : I have to crop from %s to %s" % (factor, factor, origshape, neededshape)
              self.crop(0, neededshape[0], 0, neededshape[1])
          else:
              if self.verbose :
                  print "Rebinning %ix%i : I do not need to crop" % (factor, factor)
                  
          self.numpyarray = rebin(self.numpyarray, neededshape/factor) # we call the rebin function defined below
          # The integer division neededshape/factor is ok, we checked for this above.
          self.binfactor = int(self.binfactor * factor)
docs: I robustly rebin your image by a given factor.
          You simply specify a factor, and I will eventually take care of a crop to bring
          the image to interger-multiple-of-your-factor dimensions.
          Note that if you crop your image before, you must directly crop to compatible dimensions !
          We update the binfactor, this allows you to draw on the image later, still using the
          orignial pixel coordinates.
          Here we work on the numpy array.