1+1
%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()
np.sin(3.14)
arr
arr[250][120]
image.size
width=800
height=465
rotatedArray=np.zeros((height,width))
rotatedArray
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
D2R = 3.1415/180.0
theta = 45.0*D2R
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]
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
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]
plt.imshow(shift1, cmap = cm.Greys_r)
plt.show()
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]
plt.imshow(shift2, cmap = cm.Greys_r)
plt.show()
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]
plt.imshow(shift3, cmap = cm.Greys_r)
plt.show()
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]
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
rotatedArray=ndimage.rotate(arr, 45, reshape=False)
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
def interactiveRotation(angle=0.0):
rotatedArray=ndimage.rotate(arr, angle, reshape=False)
ndimage.zoom
plt.imshow(rotatedArray, cmap = cm.Greys_r)
plt.show()
interactiveRotation(angle=20.0)
interactive(interactiveRotation, angle=(0.0, 360.0))