|
import os |
|
from PIL import Image |
|
import argparse |
|
import imageio |
|
import numpy as np |
|
import cv2 |
|
|
|
parser = argparse.ArgumentParser(description='change to useful name') |
|
parser.add_argument('--dim', default=256, type=int, help='dimention image') |
|
args = parser.parse_args() |
|
|
|
path = "." |
|
ROOT_DIR = os.path.dirname(__file__) |
|
dirs = os.listdir(ROOT_DIR) |
|
|
|
dim = args.dim |
|
|
|
|
|
def createVideo(array_pil, space_time = 5, name = "out_"): |
|
|
|
size = array_pil[0].size[0] |
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
video = cv2.VideoWriter(name +".mp4", |
|
fourcc, |
|
30, |
|
(size,size) |
|
) |
|
|
|
for image_pil in array_pil: |
|
for i in range (0,space_time): |
|
video.write(cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)) |
|
|
|
video.release() |
|
|
|
|
|
def gif_order (data, center=True): |
|
gif = [] |
|
base = 1 |
|
|
|
|
|
i = int((len(data)-2)/2) |
|
while(i > base ): |
|
gif.append(data[i]) |
|
|
|
i -= 1 |
|
|
|
|
|
|
|
gif.append(data[int((len(data)-2)/2) + 1]) |
|
|
|
|
|
|
|
if center: |
|
gif.append(data[0]) |
|
|
|
|
|
|
|
gif.append(data[int((len(data) - 2) / 2) + 2]) |
|
|
|
|
|
i = int((len(data)-2)/2) + 3 |
|
while (i < len(data)): |
|
gif.append(data[i]) |
|
|
|
i += 1 |
|
|
|
|
|
invertedgif = gif[::-1] |
|
invertedgif = invertedgif[1:] |
|
|
|
gif = gif[1:] + invertedgif |
|
|
|
|
|
|
|
|
|
return gif |
|
|
|
print(" [*] Init gif generations!") |
|
|
|
for file in dirs: |
|
if ".jpg" in file or ".png" in file: |
|
rowImages = [] |
|
im = Image.open("./" + ROOT_DIR + '/' + file) |
|
width, height = im.size |
|
|
|
|
|
|
|
|
|
pointleft = 3 |
|
pointtop = 3 |
|
i = 0 |
|
while (pointtop < height): |
|
while (pointleft < width): |
|
im1 = im.crop((pointleft, pointtop, dim+pointleft, dim+pointtop)) |
|
|
|
|
|
rowImages.append(im1) |
|
|
|
pointleft+= dim+4 |
|
|
|
rowImages = gif_order(rowImages,center=False) |
|
createVideo(rowImages, 5, name = "./" + ROOT_DIR + '/' + file[:-4] + "_" + str(i)) |
|
|
|
name = "./" + ROOT_DIR + '/' + file[:-4] + "_" + str(i) + '.gif' |
|
rowImages[0].save(name, save_all=True,format='GIF', append_images=rowImages[1:], optimize=True, duration=100, loop=0) |
|
|
|
|
|
|
|
pointtop += dim + 4 |
|
pointleft = 3 |
|
rowImages = [] |
|
i+=1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(" [*] End gif generations!") |