$29.99
Image Transformations Lab
All of the programming assignments are to be done in Python using additional libraries specified in the assignments. There are many libraries available, some of which we will be using, and you are welcome to use them with one exception: if the library or a function within it performs the specific function you are asked to code, you may not use that other than perhaps as a reference to compare against. All of the code you submit must be your own. You are welcome to turn in a completed jupyter notebook.
The following code will load an image you can use for this lab. If needed make sure to install PIL using pip install PIL or conda install PIL.
Note: On the homework, the direction of the positve Y-axis was up. In this lab (and most image packages), the direction of the positive Y-axis is down. This means that you will need to rotate in the opposite direction of what you did on the homework.
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import math
%matplotlib notebook
def compose(frame, image, transformation):
width, height = frame.size
#Invert matrix for compose function, grab values for Affine Transform
t = np.linalg.inv(transformation)
a=t[0,0]; b=t[0,1]; c=t[0,2]; d=t[1,0]; e=t[1,1]; f=t[1,2]
image = image.transform((width,height), Image.AFFINE,(a,b,c,d,e,f), Image.BICUBIC)
#Make mask from image's location
im = np.sum(np.asarray(image), -1)
vals = 255.0*( im > 0)
mask = Image.fromarray(vals).convert("1")
#Composite images together
result = Image.composite(image,frame,mask)
return result
#Open the two images
filename = "PictureFrameCollage.png"
frame = Image.open(filename).convert("RGB")
#Load all images into an array
imgs = []
for i in range(12):
imgs.append(Image.open(f"Bird{i}.png").convert("RGB"))
#Define the transformation to the first picture frame
transformation = np.matrix([[1,0,619],[0,1,433],[0,0,1]])
#Compose the two images together
result = compose(frame, imgs[0], transformation)
#Compose the rest of the images
result = compose(result, imgs[1], general_tm(tx=41, ty=30, s=1.21))
result = compose(result, imgs[2], general_tm(tx=283, ty=46, s=0.389))
result = compose(result, imgs[3], general_tm(tx=419, ty=87, d_angle=30))
result = compose(result, imgs[4], general_tm(tx=673, ty=37, d_angle=-15, s=0.653))
result = compose(result, imgs[5], general_tm(tx=350, ty=138, d_angle=-45))
result = compose(result, imgs[6], general_tm(tx=514, ty=225, s=0.389))
result = compose(result, imgs[7], general_tm(tx=633, ty=228, d_angle=15, s=0.736))
result = compose(result, imgs[8], general_tm(tx=46, ty=354, d_angle=15, s=1.21))
result = compose(result, imgs[9], ttm(305, 358) * stm(0.389) * rtm(-45))
result = compose(result, imgs[10], general_tm(tx=308, ty=463, s=0.736))
result = compose(result, imgs[11], general_tm(tx=385, ty=379, d_angle=45))
#Show the result
plt.imshow(result)
plt.show()
#Uncomment this line if you want to save the image
result.save("Output.png")
def sin(degree_angle):
return math.sin(math.radians(degree_angle))
def cos(degree_angle):
return math.cos(math.radians(degree_angle))
def stm(s):
return np.matrix([[s, 0, 0], [0, s, 0], [0, 0, 1]])
def ttm(tx, ty):
return np.matrix([[1, 0, tx], [0, 1, ty], [0, 0, 1]])
def rtm(d_angle):
return np.matrix([[cos(d_angle),sin(d_angle),0], [-sin(d_angle),cos(d_angle),0], [0,0,1]])
# This function only works in t, r, s order ONLY. This one happens most of the time so it is convenient, except for #8
def general_tm(tx=0, ty=0, d_angle=0, s=1):
return ttm(tx, ty) * rtm(d_angle) * stm(s)
Tip: Make sure you are comfortable with building your own transformations and how the compositing code works, then try implementing your own general transform function.