File size: 1,474 Bytes
84ff315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os, io
import json
import random
from PIL import Image
from tqdm import tqdm
import argparse

json_out_dir = '/path/to/json'
image_in_dir = '/path/to/image_in_dir'
image_out_dir = '/path/to/image_out_dir'

file_list = []
for root, dirs, files in os.walk(image_in_dir):
    for file in files:
        file_list.append(os.path.join(root, file))
file_list = sorted(file_list)

file_count = 0
now_file_name = image_out_dir + f'patch_{file_count:06d}'
file = open(now_file_name, 'wb')
now_btytes = file.tell()
count_image = 0
new_data = {}

for image_path in tqdm(file_list):
    image_info = {}
    btyes = 0
    sizes = []
    with open(image_path, 'rb') as img:
        img_data = img.read()
        btyes = file.write(img_data)
        sizes.append(btyes)
    patch_info = {
        "patch": now_file_name,
        "start_num": now_btytes, "size": sizes,
    }
    count_image += 1
    now_btytes = file.tell()
    if count_image == 10000:
        file.close()
        file_count += 1
        now_file_name = image_out_dir + f'patch_{file_count:06d}'
        file = open(now_file_name, 'wb')
        now_btytes = 0
        count_image = 0

    image_info['original_image_path'] = os.path.join(image_path)
    image_info['image'] = patch_info
    new_data[image_path.split('/')[-1]] = image_info
    if count_image == 1:
        print(new_data)
file.close()

with open(f'{json_out_dir}/patch_mapping.json', 'w') as f:
    json.dump(new_data, f, indent=4)