Skip to content

Instantly share code, notes, and snippets.

@saucecode
Created June 12, 2017 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saucecode/e40f274ac15587d10edff8d423a5dced to your computer and use it in GitHub Desktop.
Save saucecode/e40f274ac15587d10edff8d423a5dced to your computer and use it in GitHub Desktop.
an attempt at the three shears image rotation problem. Reads from first.png, outputs to out.png
import math
from PIL import Image, ImageDraw
input_image = Image.open("first.png")
width,height = input_image.size
second_image = Image.new('RGB', input_image.size)
third_image = Image.new('RGB', input_image.size)
output_image = Image.new('RGB', input_image.size)
# draw = ImageDraw.Draw(output_image)
theta = math.pi/8
def apply_matrix(matrix, vector):
return [ vector[0]*matrix[0][0] + vector[1]*matrix[0][1], vector[0]*matrix[1][0] + vector[1]*matrix[1][1] ]
a = [
[1, -math.tan(theta/2)],
[0, 1]
]
b = [ [1, 0], [math.sin(theta), 1] ]
c = [ [1, -math.tan(theta/2)], [0, 1] ]
for x in range(width):
for y in range(height):
mx = x - width/2
my = y - height/2
new_mx, new_my = apply_matrix(a, [mx, my])
new_mx, new_my = round(new_mx), round(new_my)
nx = new_mx + round(width/2)
ny = new_my + round(height/2)
if nx >= 0 and nx < width and ny >= 0 and ny < height:
second_image.putpixel((nx,ny), input_image.getpixel((x,y)))
for x in range(width):
for y in range(height):
mx = x - width/2
my = y - height/2
new_mx, new_my = apply_matrix(b, [mx, my])
new_mx, new_my = round(new_mx), round(new_my)
nx = new_mx + round(width/2)
ny = new_my + round(height/2)
if nx >= 0 and nx < width and ny >= 0 and ny < height:
third_image.putpixel((nx,ny), second_image.getpixel((x,y)))
for x in range(width):
for y in range(height):
mx = x - width/2
my = y - height/2
new_mx, new_my = apply_matrix(c, [mx, my])
new_mx, new_my = round(new_mx), round(new_my)
nx = new_mx + round(width/2)
ny = new_my + round(height/2)
if nx >= 0 and nx < width and ny >= 0 and ny < height:
output_image.putpixel((nx,ny), third_image.getpixel((x,y)))
output_image.save('out.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment