In [1]:
1+1
Out[1]:
2
In [2]:
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from PIL import Image
from scipy import ndimage
from IPython.html.widgets import interact, interactive

fname = '/tmp/batman.png'
image = Image.open(fname)
arr = np.asarray(image)
plt.imshow(arr, cmap = cm.Greys_r)
plt.show()
In [3]:
np.sin(3.14)
Out[3]:
0.0015926529164868282
In [4]:
arr
Out[4]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
In [5]:
arr[250][120]
Out[5]:
255
In [7]:
image.size
Out[7]:
(800, 465)
In [8]:
width=800
height=465
rotatedArray=np.zeros((height,width))
In [11]:
rotatedArray
Out[11]:
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])
In [12]:
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
In [37]:
D2R = 3.1415/180.0
theta = 45.0*D2R
In [38]:
rotatedArray=np.zeros((height,width))
for x in range(0,width-1):
    for y in range(0,height-1):
        newX = round(np.cos(theta)*(x-width/2)+np.sin(theta)*(y-height/2))+width/2
        newY = round(-np.sin(theta)*(x-width/2)+np.cos(theta)*(y-height/2))+height/2
        if 0 <= newY < height and 0 <= newX < width:
            rotatedArray[newY][newX]=arr[y][x]
In [39]:
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
In [40]:
shift1=np.zeros((2*height,2*width))
for x in range(0,width-1):
    for y in range(0,height-1):
        newX = x+np.tan(theta/2.0)*y+width/2-np.tan(theta/2.0)*(height/2)
        newY = y+height/2
        if 0 <= newY < 2*height and 0 <= newX < 2*width:
            shift1[newY][newX]=arr[y][x]
In [41]:
plt.imshow(shift1, cmap = cm.Greys_r)
plt.show()
In [42]:
shift2=np.zeros((2*height,2*width))
for x in range(0,2*width-1):
    for y in range(0,2*height-1):
        newX = x
        newY = -np.sin(theta)*x+y+np.sin(theta)*width
        if 0 <= newY < 2*height and 0 <= newX < 2*width:
            shift2[newY][newX]=shift1[y][x]
In [43]:
plt.imshow(shift2, cmap = cm.Greys_r)
plt.show()
In [44]:
shift3=np.zeros((2*height,2*width))
for x in range(0,2*width-1):
    for y in range(0,2*height-1):
        newX = x+np.tan(theta/2.0)*y-np.tan(theta/2.0)*height
        newY = y
        if 0 <= newY < 2*height and 0 <= newX < 2*width:
            shift3[newY][newX]=shift2[y][x]
In [45]:
plt.imshow(shift3, cmap = cm.Greys_r)
plt.show()
In [46]:
rotatedArray=np.zeros((height,width))
for x in range(0,width-1):
    for y in range(0,height-1):
        rotatedArray[y][x]=shift3[y+height/2][x+width/2]
In [47]:
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
In [9]:
rotatedArray=ndimage.rotate(arr, 45, reshape=False)
In [10]:
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
In [11]:
def interactiveRotation(angle=0.0):
    rotatedArray=ndimage.rotate(arr, angle, reshape=False)
    ndimage.zoom
    plt.imshow(rotatedArray, cmap = cm.Greys_r)
    plt.show()
In [12]:
interactiveRotation(angle=20.0)
In [13]:
interactive(interactiveRotation, angle=(0.0, 360.0))
In []: