File size: 3,419 Bytes
ac60157 deee42b fb15799 0f9a3a7 fb15799 0f9a3a7 fb15799 0f9a3a7 fb15799 0f9a3a7 27ca4d1 0f9a3a7 fb15799 ef8e380 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import os
os.system("pip install gradio==2.9.4")
import gradio as gr
import numpy as np
import cv2 as cv
import sys
import math
"""
The following rotate function is only included for reference and is not called.
This shows how the recursion works, but does not provide any sort of interesting animation.
"""
def rotate(img, x, y, width):
width = width // 2
#divides the image into four region and slides them clockwise
temp = np.copy(img[y:y + width, x:x + width])
img[y:y + width, x:x + width] = img[y + width:y + 2 * width, x:x + width]
img[y + width:y + 2 * width, x:x + width] = img[y + width:y + 2 * width, x + width:x + 2 * width]
img[y + width:y + 2 * width, x + width:x + 2 * width] = img[y:y + width, x + width:x + 2 * width]
img[y:y + width, x + width:x + 2 * width] = temp
if width > 1:
#recusively rotating the tiles
rotate(img, x, y, width)
rotate(img, x + width, y, width)
rotate(img, x + width, y + width, width)
rotate(img, x, y + width, width)
def rotate_with_steps(img, output, x, y, width, shift):
temp = np.copy(img[y:y + width, x:x + width])
output[y + width - shift:y + 2 * width - shift, x:x + width] = img[y + width:y + 2 * width, x:x + width]
output[y + width:y + 2 * width, x + width - shift:x + 2 * width - shift] = img[y + width:y + 2 * width, x + width:x + 2 * width]
output[y + shift:y + width + shift, x + width:x + 2 * width] = img[y:y + width, x + width:x + 2 * width]
output[y:y + width, x + shift:x + width + shift] = temp
def is_power_of_two(n):
return (n != 0) and (n & (n-1) == 0)
#read in the image
def run(image):
image = cv.imread(image)
#check if image is of the right size
if not (is_power_of_two(image.shape[0]) and image.shape[0] == image.shape[1]):
print("The image you have provided does not have dimensions N x N where N is a power of 2.")
closest_valid_dimension = 2**round(math.log(min(image.shape[0], image.shape[1]), 2))
#prompt user to either resize image to closest NxN where N is a power of 2
image = cv.resize(image, (closest_valid_dimension, closest_valid_dimension))
img_dim = image.shape[0]
#make a video writer
out_file_name = "out.mp4"
out = cv.VideoWriter(out_file_name, cv.VideoWriter_fourcc('m','p','4','v'), 30, (img_dim, img_dim))
#rotate the image 4 times
for rotations in range(4):
width = img_dim // 2
number_of_frames = 2 * int(math.log2(img_dim))
while width > 0:
number_of_frames -= 2
number_of_frames = max(1, number_of_frames)
#create the sliding animation by gradually moving the tiles
for i in range(0, number_of_frames):
output_canvas = np.copy(image)
shift = (width * (i + 1)) // number_of_frames
for x in range(0, img_dim, 2 * width):
for y in range(0, img_dim, 2 * width):
rotate_with_steps(image, output_canvas, x, y, width, shift)
out.write(output_canvas)
image = np.copy(output_canvas)
width = width // 2
out.release()
return "out.mp4"
gr.Interface(run,gr.inputs.Image(type="filepath"),"playable_video").launch()
|