keiphone commited on
Commit
c6e05c6
1 Parent(s): 3cdc44f

Upload 3 files

Browse files
Files changed (3) hide show
  1. download_imgs.py +27 -0
  2. split_tiled_imgs.py +49 -0
  3. webset.html +0 -0
download_imgs.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import requests
3
+ from tqdm import tqdm
4
+ with open("preprocess_CustomConcept101/webset.html",'r') as f:
5
+ lines = f.readlines()
6
+
7
+ lines = [l.strip().strip('\n') for l in lines]
8
+ img_urls = []
9
+ for line in lines:
10
+ if "images/benchmark/" in line:
11
+ if ("images/benchmark/custom_diffusion" in line) or ("images/benchmark/dreambooth" in line) or ("images/benchmark/ti_" in line):
12
+ continue
13
+ s = line.find("images/benchmark/")
14
+ e = line.find(".jpg")
15
+ # https://www.cs.cmu.edu/~custom-diffusion/images/benchmark/actionfigure_1.jpg
16
+ url = "https://www.cs.cmu.edu/~custom-diffusion/" + line[s:e] + ".jpg"
17
+ img_urls.append(url)
18
+ if len(img_urls) < 10:
19
+ print(url)
20
+ print(len(img_urls))
21
+
22
+ save_dir = "preprocess_CustomConcept101/tiled_imgs_per_concept/"
23
+ for img_url in tqdm(img_urls):
24
+ filename = img_url.split('/')[-1]
25
+ img_data = requests.get(img_url).content
26
+ with open(save_dir+filename, 'wb') as handler:
27
+ handler.write(img_data)
split_tiled_imgs.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import os
3
+ from tqdm import tqdm
4
+
5
+ def split_and_save_tiled_image(image_path,global_idx,class_name,save_dir):
6
+ # Open the image
7
+ img = Image.open(image_path)
8
+
9
+ # Get the dimensions of the tiled image
10
+ width, height = img.size
11
+ assert height == 256
12
+ assert width % 256 == 0
13
+
14
+ tile_width = 256
15
+ num_tiles = width // tile_width
16
+
17
+ # Iterate through each tile
18
+ for i in range(num_tiles):
19
+ # Calculate the starting and ending x-coordinates for the tile
20
+ start_x = i * tile_width
21
+ end_x = start_x + tile_width
22
+
23
+ # Crop the tile from the original image
24
+ tile = img.crop((start_x, 0, end_x, height))
25
+
26
+ # Save the tile with its index as filename
27
+ filename = f"{global_idx}.{i}.{num_tiles}.{class_name}.jpg" # # id.n.N.ClassName.jpg
28
+ tile.save(os.path.join(save_dir,filename)) # You can choose the desired image format
29
+ global_idx += 1
30
+ return global_idx
31
+
32
+
33
+
34
+
35
+ if __name__ == "__main__":
36
+ # id.n.N.ClassName.jpg
37
+ load_dir = "preprocess_CustomConcept101/tiled_imgs_per_concept"
38
+ save_dir = "preprocess_CustomConcept101/splited_imgs"
39
+
40
+ filenames = sorted(os.listdir(load_dir))
41
+ global_idx = 0
42
+ for filename in tqdm(filenames):
43
+ img_path = os.path.join(load_dir,filename)
44
+ class_name = filename.split('.')[0].split('_')[0] # filename can be "jacket1" or "person_1" or "lighthouse"
45
+ class_name = [c for c in class_name if c not in "0123456789"]
46
+ class_name = "".join(class_name)
47
+ # print(class_name)
48
+ global_idx = split_and_save_tiled_image(img_path,global_idx,class_name,save_dir)
49
+
webset.html ADDED
The diff for this file is too large to render. See raw diff