Rodrigo_Cobo
fixed video issues
8eba3ec
raw
history blame
3.44 kB
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", # Filename
fourcc,
30, # 10 frames per second is chosen as a demo, 30FPS and 60FPS is more typical for a YouTube video
(size,size) # The width and height come from the stats of image1
)
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
#primera mitad
i = int((len(data)-2)/2)
while(i > base ):
gif.append(data[i])
#print(i)
i -= 1
#el del medio izq
gif.append(data[int((len(data)-2)/2) + 1])
#print(int((len(data)-2)/2) + 1)
#el inicial
if center:
gif.append(data[0])
#print(0)
# el del medio der
gif.append(data[int((len(data) - 2) / 2) + 2])
#print(int((len(data) - 2) / 2) +2)
#segunda mitad
i = int((len(data)-2)/2) + 3
while (i < len(data)):
gif.append(data[i])
#print(i)
i += 1
#print("---------")
invertedgif = gif[::-1]
invertedgif = invertedgif[1:]
gif = gif[1:] + invertedgif
#print(gif)
#for image in gif:
# image.show()
#gsdfgsfgf
return gif
print(" [*] Init gif generations!")
# This would print all the files and directories
for file in dirs:
if ".jpg" in file or ".png" in file:
rowImages = []
im = Image.open("./" + ROOT_DIR + '/' + file)
width, height = im.size
#im = im.convert('P')
#CROP (left, top, right, bottom)
pointleft = 3
pointtop = 3
i = 0
while (pointtop < height):
while (pointleft < width):
im1 = im.crop((pointleft, pointtop, dim+pointleft, dim+pointtop))
#h1 = im1.size[0] * 4
#im1 = im1.resize([h1,h1], Image.Resampling.LANCZOS)
rowImages.append(im1)
#im1.show()
pointleft+= dim+4
# Ya tengo todas las imagenes podria hacer el gif aca
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)
#print(rowImages)
pointtop += dim + 4
pointleft = 3
rowImages = []
i+=1
#im2 = im.crop((width / 2, 0, width, height))
# im2.show()
#im1.save("./2" + file[:-4] + ".png")
#im2.save("./" + file[:-4] + ".png")
# Deleted
#os.remove("data/" + file)
print(" [*] End gif generations!")