Diagonal rotation


Sage Cython: To run, type %attach PicDiagRotate.pyx and OK() at the Sage command line, or paste into a Sage notebook with %cython as the first line.

#PicDiagRotate.pyx
import numpy as np
cimport numpy as np
from scipy import misc
DEF side=512
DEF halfside=side/2 
cpdef OK():
	cdef int i,w,row,loops,lps,L,R,t[3]
	cdef np.ndarray[np.uint8_t, ndim=3] a
	fn="tiger-09.png" #a 512x512 colour png
	a=misc.imread(fn)
	for w in xrange(1,halfside+1):
		for row in xrange(w,halfside+1): 
#row 1 is the 4 pixels at centre, 256 is around edge of pic.
			if row>1: #do twice more for each row outside the first
				loops=2
			else:
				loops=1
			for lps in xrange(loops):
				L=halfside-row
				R=side-1-L
				for i in xrange(3): #store temp pixel
					t[i]=a[L][L][i]
				for i in xrange(L,R,1):	#move left column down
					a[L][i]=a[L][i+1]
				for i in xrange(L,R): #move top row to the left
					a[i][R]=a[i+1][R]
				for i in xrange(R,L,-1): #move right column up
					a[R][i]=a[R][i-1] 
				for i in xrange(R,L,-1):#move bottom row to right
					a[i][L]=a[i-1][L]
				for i in xrange(3): #put temp back
					a[L+1][L][i]=t[i]
		print w
		misc.imsave('tpics/%05d.png' % w,a) #NB make a folder called 'tpics' first.