File size: 3,105 Bytes
8eba3ec 604f38b 8eba3ec 604f38b 8eba3ec 604f38b 8eba3ec |
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import os
from PIL import Image
import argparse
import imageio
import numpy as np
import cv2
import mediapy as media
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_"):
ar_im = []
for j in range(3):
for image in array_pil:
for i in range (0,space_time):
ar_im.append(np.array(image))
media.write_video(name +".mp4" , ar_im, fps=30, qp=18)
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, 3, 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!") |