title: How can I use scipy.ndimage.interpolation.affine_transform to rotate an image about its centre?
body: <p>I am perplexed by the API to <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.interpolation.affine_transform.html" rel="noreferrer"><code>scipy.ndimage.interpolation.affine_transform</code></a>.  And judging by <a href="https://github.com/scipy/scipy/issues/2255" rel="noreferrer">this issue</a> I'm not the only one.  I'm actually wanting to do more interesting things with <code>affine_transform</code> than just rotating an image, but a rotation would do for starters.  (And yes I'm well aware of <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.interpolation.rotate.html" rel="noreferrer"><code>scipy.ndimage.interpolation.rotate</code></a>, but figuring out how to drive <code>affine_transform</code> is what interests me here).</p>
  
  <p>When I want to do this sort of thing in systems like OpenGL, I'm think in terms of computing the transform which applies a 2x2 rotation matrix <code>R</code> about a centre <code>c</code>, and therefore thinking of points <code>p</code> being transformed <code>(p-c)R+c</code> = <code>pR+c-cR</code>, which gives a <code>c-cR</code> term to be used as the translation component of a transform.  <strong>However</strong>, according to the issue above, scipy's <code>affine_transform</code> does "<em>offset first</em>" so we actually need to compute an offset <code>s</code> such that <code>(p-c)R+c=(p+s)R</code> which with a bit of rearrangement gives <code>s=(c-cR)R'</code> where <code>R'</code> is the inverse of <code>R</code>.</p>
  
  <p>If I plug this into an ipython notebook (pylab mode; code below maybe needs some additional imports):</p>
  
  <pre><code>img=scipy.misc.lena()
  #imshow(img,cmap=cm.gray);show()
  centre=0.5*array(img.shape)
  a=15.0*pi/180.0
  rot=array([[cos(a),sin(a)],[-sin(a),cos(a)]])
  offset=(centre-centre.dot(rot)).dot(linalg.inv(rot))
  rotimg=scipy.ndimage.interpolation.affine_transform(
      img,rot,order=2,offset=offset,cval=0.0,output=float32
  )
  imshow(rotimg,cmap=cm.gray);show()
  </code></pre>
  
  <p>I get </p>
  
  <p><img src="https://i.stack.imgur.com/L4UVa.png" alt="rotated but not about centre lena image"></p>
  
  <p>which unfortunately isn't rotated about the centre.</p>
  
  <p>So what's the trick I'm missing here?</p>