Skip to content

Commit e79f40a

Browse files
committed
Adjust tqdm for batch
1 parent 73373ef commit e79f40a

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

gbmi/utils/images.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ def batch_run(
9191
stdout_write: Optional[Callable] = None,
9292
stderr_write: Optional[Callable] = None,
9393
wrap_errs: Optional[list[Exception]] = None,
94+
tqdm_position: Optional[int] = 0,
95+
tqdm_desc: Optional[str] = None,
9496
**kwargs,
9597
):
98+
if tqdm_position is None:
99+
tqdm_position = 0
96100
if len(images) > batchsize:
97101
return [
98102
batch_run(
@@ -102,9 +106,17 @@ def batch_run(
102106
post_args=post_args,
103107
check=check,
104108
wrap_errs=wrap_errs,
109+
tqdm_position=tqdm_position + 1,
110+
tqdm_desc=tqdm_desc,
111+
stdout_write=stdout_write or partial(tqdm.write, file=sys.stdout),
112+
stderr_write=stderr_write or partial(tqdm.write, file=sys.stderr),
105113
**kwargs,
106114
)
107-
for i in range(0, len(images), batchsize)
115+
for i in tqdm(
116+
range(0, len(images), batchsize),
117+
desc=f"Batch{' for {tqdm_desc}' if tqdm_desc else ''}",
118+
position=tqdm_position,
119+
)
108120
]
109121

110122
if stdout_write is None:
@@ -166,6 +178,7 @@ def ect(
166178
stdout_write: Optional[Callable] = None,
167179
stderr_write: Optional[Callable] = None,
168180
wrap_errs: Optional[list[Exception]] = None,
181+
tqdm_position: Optional[int] = None,
169182
):
170183
if not images:
171184
return
@@ -186,6 +199,7 @@ def ect(
186199
stdout_write=stdout_write,
187200
stderr_write=stderr_write,
188201
wrap_errs=wrap_errs,
202+
tqdm_position=tqdm_position,
189203
)
190204

191205

@@ -209,6 +223,7 @@ def optipng(
209223
stdout_write: Optional[Callable] = None,
210224
stderr_write: Optional[Callable] = None,
211225
wrap_errs: Optional[list[Exception]] = None,
226+
tqdm_position: Optional[int] = None,
212227
):
213228
if not images:
214229
return
@@ -225,6 +240,7 @@ def optipng(
225240
stdout_write=stdout_write,
226241
stderr_write=stderr_write,
227242
wrap_errs=wrap_errs,
243+
tqdm_position=tqdm_position,
228244
)
229245

230246

@@ -241,6 +257,7 @@ def pngcrush(
241257
stdout_write: Optional[Callable] = None,
242258
stderr_write: Optional[Callable] = None,
243259
wrap_errs: Optional[list[Exception]] = None,
260+
tqdm_position: Optional[int] = None,
244261
):
245262
if not images:
246263
return
@@ -276,6 +293,7 @@ def pngcrush(
276293
stdout_write=stdout_write,
277294
stderr_write=stderr_write,
278295
wrap_errs=wrap_errs,
296+
tqdm_position=tqdm_position,
279297
)
280298

281299
# Replace original images with crushed images if they are smaller
@@ -296,7 +314,7 @@ def optimize(
296314
tmpdir: Optional[Union[str, Path]] = None,
297315
cleanup: Optional[bool] = None,
298316
trim_printout: bool = False,
299-
tqdm_position: Optional[int] = None,
317+
tqdm_position: int = 0,
300318
tqdm_leave: Optional[bool] = None,
301319
stdout_write: Optional[Callable] = None,
302320
stderr_write: Optional[Callable] = None,
@@ -316,6 +334,7 @@ def optimize(
316334
stdout_write=partial(tqdm.write, file=sys.stdout),
317335
stderr_write=partial(tqdm.write, file=sys.stderr),
318336
wrap_errs=wrap_errs,
337+
tqdm_position=tqdm_position + 1,
319338
)
320339
optipng(
321340
*cur_images,
@@ -324,6 +343,7 @@ def optimize(
324343
stdout_write=stdout_write,
325344
stderr_write=stderr_write,
326345
wrap_errs=wrap_errs,
346+
tqdm_position=tqdm_position,
327347
)
328348
pngcrush(
329349
*cur_images,
@@ -334,6 +354,7 @@ def optimize(
334354
stdout_write=stdout_write,
335355
stderr_write=stderr_write,
336356
wrap_errs=wrap_errs,
357+
tqdm_position=tqdm_position,
337358
)
338359
new_sizes = [Path(image).stat().st_size for image in cur_images]
339360
cur_images = [

0 commit comments

Comments
 (0)