@@ -35,6 +35,84 @@ def preprocess(process_src, process_dst, process_width, process_height, preproce
35
35
deepbooru .release_process ()
36
36
37
37
38
+ def listfiles (dirname ):
39
+ return os .listdir (dirname )
40
+
41
+
42
+ class PreprocessParams :
43
+ src = None
44
+ dstdir = None
45
+ subindex = 0
46
+ flip = False
47
+ process_caption = False
48
+ process_caption_deepbooru = False
49
+ preprocess_txt_action = None
50
+
51
+
52
+ def save_pic_with_caption (image , index , params : PreprocessParams , existing_caption = None ):
53
+ caption = ""
54
+
55
+ if params .process_caption :
56
+ caption += shared .interrogator .generate_caption (image )
57
+
58
+ if params .process_caption_deepbooru :
59
+ if len (caption ) > 0 :
60
+ caption += ", "
61
+ caption += deepbooru .get_tags_from_process (image )
62
+
63
+ filename_part = params .src
64
+ filename_part = os .path .splitext (filename_part )[0 ]
65
+ filename_part = os .path .basename (filename_part )
66
+
67
+ basename = f"{ index :05} -{ params .subindex } -{ filename_part } "
68
+ image .save (os .path .join (params .dstdir , f"{ basename } .png" ))
69
+
70
+ if params .preprocess_txt_action == 'prepend' and existing_caption :
71
+ caption = existing_caption + ' ' + caption
72
+ elif params .preprocess_txt_action == 'append' and existing_caption :
73
+ caption = caption + ' ' + existing_caption
74
+ elif params .preprocess_txt_action == 'copy' and existing_caption :
75
+ caption = existing_caption
76
+
77
+ caption = caption .strip ()
78
+
79
+ if len (caption ) > 0 :
80
+ with open (os .path .join (params .dstdir , f"{ basename } .txt" ), "w" , encoding = "utf8" ) as file :
81
+ file .write (caption )
82
+
83
+ params .subindex += 1
84
+
85
+
86
+ def save_pic (image , index , params , existing_caption = None ):
87
+ save_pic_with_caption (image , index , params , existing_caption = existing_caption )
88
+
89
+ if params .flip :
90
+ save_pic_with_caption (ImageOps .mirror (image ), index , params , existing_caption = existing_caption )
91
+
92
+
93
+ def split_pic (image , inverse_xy , width , height , overlap_ratio ):
94
+ if inverse_xy :
95
+ from_w , from_h = image .height , image .width
96
+ to_w , to_h = height , width
97
+ else :
98
+ from_w , from_h = image .width , image .height
99
+ to_w , to_h = width , height
100
+ h = from_h * to_w // from_w
101
+ if inverse_xy :
102
+ image = image .resize ((h , to_w ))
103
+ else :
104
+ image = image .resize ((to_w , h ))
105
+
106
+ split_count = math .ceil ((h - to_h * overlap_ratio ) / (to_h * (1.0 - overlap_ratio )))
107
+ y_step = (h - to_h ) / (split_count - 1 )
108
+ for i in range (split_count ):
109
+ y = int (y_step * i )
110
+ if inverse_xy :
111
+ splitted = image .crop ((y , 0 , y + to_h , to_w ))
112
+ else :
113
+ splitted = image .crop ((0 , y , to_w , y + to_h ))
114
+ yield splitted
115
+
38
116
39
117
def preprocess_work (process_src , process_dst , process_width , process_height , preprocess_txt_action , process_flip , process_split , process_caption , process_caption_deepbooru = False , split_threshold = 0.5 , overlap_ratio = 0.2 , process_focal_crop = False , process_focal_crop_face_weight = 0.9 , process_focal_crop_entropy_weight = 0.3 , process_focal_crop_edges_weight = 0.5 , process_focal_crop_debug = False ):
40
118
width = process_width
@@ -48,82 +126,28 @@ def preprocess_work(process_src, process_dst, process_width, process_height, pre
48
126
49
127
os .makedirs (dst , exist_ok = True )
50
128
51
- files = os . listdir (src )
129
+ files = listfiles (src )
52
130
53
131
shared .state .textinfo = "Preprocessing..."
54
132
shared .state .job_count = len (files )
55
133
56
- def save_pic_with_caption (image , index , existing_caption = None ):
57
- caption = ""
58
-
59
- if process_caption :
60
- caption += shared .interrogator .generate_caption (image )
61
-
62
- if process_caption_deepbooru :
63
- if len (caption ) > 0 :
64
- caption += ", "
65
- caption += deepbooru .get_tags_from_process (image )
66
-
67
- filename_part = filename
68
- filename_part = os .path .splitext (filename_part )[0 ]
69
- filename_part = os .path .basename (filename_part )
70
-
71
- basename = f"{ index :05} -{ subindex [0 ]} -{ filename_part } "
72
- image .save (os .path .join (dst , f"{ basename } .png" ))
73
-
74
- if preprocess_txt_action == 'prepend' and existing_caption :
75
- caption = existing_caption + ' ' + caption
76
- elif preprocess_txt_action == 'append' and existing_caption :
77
- caption = caption + ' ' + existing_caption
78
- elif preprocess_txt_action == 'copy' and existing_caption :
79
- caption = existing_caption
80
-
81
- caption = caption .strip ()
82
-
83
- if len (caption ) > 0 :
84
- with open (os .path .join (dst , f"{ basename } .txt" ), "w" , encoding = "utf8" ) as file :
85
- file .write (caption )
86
-
87
- subindex [0 ] += 1
88
-
89
- def save_pic (image , index , existing_caption = None ):
90
- save_pic_with_caption (image , index , existing_caption = existing_caption )
91
-
92
- if process_flip :
93
- save_pic_with_caption (ImageOps .mirror (image ), index , existing_caption = existing_caption )
94
-
95
- def split_pic (image , inverse_xy ):
96
- if inverse_xy :
97
- from_w , from_h = image .height , image .width
98
- to_w , to_h = height , width
99
- else :
100
- from_w , from_h = image .width , image .height
101
- to_w , to_h = width , height
102
- h = from_h * to_w // from_w
103
- if inverse_xy :
104
- image = image .resize ((h , to_w ))
105
- else :
106
- image = image .resize ((to_w , h ))
107
-
108
- split_count = math .ceil ((h - to_h * overlap_ratio ) / (to_h * (1.0 - overlap_ratio )))
109
- y_step = (h - to_h ) / (split_count - 1 )
110
- for i in range (split_count ):
111
- y = int (y_step * i )
112
- if inverse_xy :
113
- splitted = image .crop ((y , 0 , y + to_h , to_w ))
114
- else :
115
- splitted = image .crop ((0 , y , to_w , y + to_h ))
116
- yield splitted
117
-
134
+ params = PreprocessParams ()
135
+ params .dstdir = dst
136
+ params .flip = process_flip
137
+ params .process_caption = process_caption
138
+ params .process_caption_deepbooru = process_caption_deepbooru
139
+ params .preprocess_txt_action = preprocess_txt_action
118
140
119
141
for index , imagefile in enumerate (tqdm .tqdm (files )):
120
- subindex = [ 0 ]
142
+ params . subindex = 0
121
143
filename = os .path .join (src , imagefile )
122
144
try :
123
145
img = Image .open (filename ).convert ("RGB" )
124
146
except Exception :
125
147
continue
126
148
149
+ params .src = filename
150
+
127
151
existing_caption = None
128
152
existing_caption_filename = os .path .splitext (filename )[0 ] + '.txt'
129
153
if os .path .exists (existing_caption_filename ):
@@ -143,8 +167,8 @@ def split_pic(image, inverse_xy):
143
167
process_default_resize = True
144
168
145
169
if process_split and ratio < 1.0 and ratio <= split_threshold :
146
- for splitted in split_pic (img , inverse_xy ):
147
- save_pic (splitted , index , existing_caption = existing_caption )
170
+ for splitted in split_pic (img , inverse_xy , width , height , overlap_ratio ):
171
+ save_pic (splitted , index , params , existing_caption = existing_caption )
148
172
process_default_resize = False
149
173
150
174
if process_focal_crop and img .height != img .width :
@@ -165,11 +189,11 @@ def split_pic(image, inverse_xy):
165
189
dnn_model_path = dnn_model_path ,
166
190
)
167
191
for focal in autocrop .crop_image (img , autocrop_settings ):
168
- save_pic (focal , index , existing_caption = existing_caption )
192
+ save_pic (focal , index , params , existing_caption = existing_caption )
169
193
process_default_resize = False
170
194
171
195
if process_default_resize :
172
196
img = images .resize_image (1 , img , width , height )
173
- save_pic (img , index , existing_caption = existing_caption )
197
+ save_pic (img , index , params , existing_caption = existing_caption )
174
198
175
- shared .state .nextjob ()
199
+ shared .state .nextjob ()
0 commit comments