File size: 2,338 Bytes
cc6c676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from PIL import Image
import argparse

parser = argparse.ArgumentParser(description='change to useful name')
parser.add_argument('--dim', default=128, type=int, help='dimention image')
args = parser.parse_args()

path = "."
dirs = os.listdir(path)

dim = args.dim

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


# This would print all the files and directories
for file in dirs:
    if ".jpg" in file or ".png" in file:
        rowImages = []
        im = Image.open("./" + file)
        width, height = im.size
        im = im.convert('RGB')

        #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))
                rowImages.append(im1.quantize())
                #im1.show()
                pointleft+= dim+4
            # Ya tengo todas las imagenes podria hacer el gif aca
            rowImages = gif_order(rowImages,center=False)
            name = 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
        #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)