Skip to content

Commit

Permalink
warp tests have been added
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres committed Nov 21, 2018
1 parent a16ac61 commit cbf4af1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 87 deletions.
27 changes: 16 additions & 11 deletions Thereal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ def clamp(value, minimum, maximum):
# @param points list of (x, y, dx, dy) tuples
# @return warped image

def warp(image, points):
def warp(image, point):
result = img = Image.new("RGB",image.size,"black")

if point[0] > image.size[0] or point[2] > image.size[0] or point[1] > image.size[1] or point[3] > image.size[1]:
print("Point given is out of range")
return
image_pixels = image.load()
result_pixels = result.load()

Expand All @@ -27,21 +29,24 @@ def warp(image, points):

offset = [0,0]

for point in points:
point_position = (point[0] + point[2],point[1] + point[3])
shift_vector = (point[2],point[3])
point_position = (point[0] + point[2],point[1] + point[3])
shift_vector = (point[2],point[3])

# warping formula
helper = 1.0 / (3 * (points_distance((x,y),point_position) / vector_length(shift_vector)) ** 4 + 1)

helper = 1.0 / (3 * (points_distance((x,y),point_position) / vector_length(shift_vector)) ** 4 + 1)

offset[0] -= helper * shift_vector[0]
offset[1] -= helper * shift_vector[1]
offset[0] -= helper * shift_vector[0]
offset[1] -= helper * shift_vector[1]

# coordinates for new pixels
coords = (clamp(x + int(offset[0]),0,image.size[0] - 1),clamp(y + int(offset[1]),0,image.size[1] - 1))

result_pixels[x,y] = image_pixels[coords[0],coords[1]]

return result

image = Image.open("tester.png")
image = warp(image,[(210,296,100,0), (101,97,-30,-10), (77,473,50,-100)])
image.save("testwarp.png","PNG")
image = Image.open("movie.png")
print("Now warping image...")
image = warp(image, (890, 590, 200, 300))
image.save("testwarp_two.png","PNG")
Binary file added testwarp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testwarp_two.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 35 additions & 76 deletions warp.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,52 @@
# Imported PIL Library from PIL import Image
from PIL import Image
import math
# Open an Image
def open_image(path):
newImage = Image.open(path)
return newImage

# Save Image
def save_image(image, path):
image.save(path, 'png')
def vector_length(vector):
return math.sqrt(vector[0] ** 2 + vector[1] ** 2)

def points_distance(point1, point2):
return vector_length((point1[0] - point2[0],point1[1] - point2[1]))

# Create a new image with the given size
def create_image(i, j):
image = Image.new("RGB", (i, j), "white")
return image
def clamp(value, minimum, maximum):
return max(min(value,maximum),minimum)

## Warps an image accoording to given points and shift vectors.
#
# @param image input image
# @param points list of (x, y, dx, dy) tuples
# @return warped image

# Get the pixel from the given image
def get_pixel(image, i, j):
# Inside image bounds?
width, height = image.size
if i > width or j > height:
return None

# Get Pixel
pixel = image.getpixel((i, j))
return pixel


#x,y differences
def pointsdistance(point1, point2):
return (point1[0] - point2[0],point1[1] - point2[1])

#warp a image given an image and two points
#p1 and p2 are x, y values
def warp(image, point):
newpix = Image.new("RGB", image.size, "white")
result = img = Image.new("RGB",image.size,"black")
if point[0] > image.size[0] or point[2] > image.size[0] or point[1] > image.size[1] or point[3] > image.size[1]:
print("Point given is out of range")
return
image_pixels = image.load()
result_pixels = result.load()

for y in range(image.size[1]):
for x in range(image.size[0]):

orgpix = image.load()
newpix = newpix.load()
offset = [0,0]

point_position = (point[0] + point[2],point[1] + point[3])
shift_vector = (point[2],point[3])

for x in range(image.size[0]):
for y in range(image.size[1]):
if (x > point[0] and x< point[2]) and (y > point[1] and y< point[3]):
newpix[x,y] = orgpix[point[0], point[1]]
newpix[x,y] = orgpix[x,y]
# warping formula
helper = 1.0 / (3 * (points_distance((x,y),point_position) / vector_length(shift_vector)) ** 4 + 1)


return newpix
offset[0] -= helper * shift_vector[0]
offset[1] -= helper * shift_vector[1]

# coordinates for new pixels
coords = (clamp(x + int(offset[0]),0,image.size[0] - 1),clamp(y + int(offset[1]),0,image.size[1] - 1))

result_pixels[x,y] = image_pixels[coords[0],coords[1]]

def change_background(image, background):
width, height = image.size
new = create_image(width, height)
pixels = new.load()
print(image.size, background.size)
for i in range(width):
for j in range(height):
pixel = get_pixel(image, i, j)
red = pixel[0]
green = pixel[1]
blue = pixel[2]
try:
pixel_background = get_pixel(background, i, j)
red_b = pixel_background[0]
green_b = pixel_background[1]
blue_b = pixel_background[2]
except:
pass

if green > 70 and red > 10 and blue > 30 and blue < 60 and red < 60: # greenscreen
pixels[i,j] = (int(red_b), int(green_b), int(blue_b))

else:
pixels[i,j] = (int(red), int(green), int(blue))

return new

return result

# Main
if __name__ == "__main__":
# Load Image (JPEG/JPG needs libjpeg to load)
original = open_image('test.png')
#background = open_image('eiffel_tower.png')
warped = warp(original, (101, 97, -30, -10))
print('Now converting...')
#new = change_background(original, background)
#print('Done')
save_image(warped, 'warped.png')
image = Image.open("movie.png")
print("Now warping image...")
image = warp(image, (890, 590, 200, 300))
image.save("testwarp_two.png","PNG")

0 comments on commit cbf4af1

Please sign in to comment.