File size: 1,608 Bytes
c6e05c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9863bad
 
c6e05c6
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
import os
from tqdm import tqdm

def split_and_save_tiled_image(image_path,global_idx,class_name,save_dir):
    # Open the image
    img = Image.open(image_path)

    # Get the dimensions of the tiled image
    width, height = img.size
    assert height == 256
    assert width % 256 == 0

    tile_width = 256
    num_tiles = width // tile_width

    # Iterate through each tile
    for i in range(num_tiles):
        # Calculate the starting and ending x-coordinates for the tile
        start_x = i * tile_width
        end_x = start_x + tile_width

        # Crop the tile from the original image
        tile = img.crop((start_x, 0, end_x, height))

        # Save the tile with its index as filename
        filename = f"{global_idx}.{i}.{num_tiles}.{class_name}.jpg"  # # id.n.N.ClassName.jpg
        tile.save(os.path.join(save_dir,filename))  # You can choose the desired image format
        global_idx += 1
    return global_idx




if __name__ == "__main__":
    # id.n.N.ClassName.jpg
    load_dir = "./tiled_imgs_per_concept"
    save_dir = "./customconcept256x256"

    filenames = sorted(os.listdir(load_dir))
    global_idx = 0
    for filename in tqdm(filenames):
        img_path = os.path.join(load_dir,filename)
        class_name = filename.split('.')[0].split('_')[0] # filename can be "jacket1" or "person_1" or "lighthouse"
        class_name = [c for c in class_name if c not in "0123456789"]
        class_name = "".join(class_name)
        # print(class_name)
        global_idx = split_and_save_tiled_image(img_path,global_idx,class_name,save_dir)