Skip to content

Commit 8afe67c

Browse files
committed
feat: add webp conversion to balatrobench cli
1 parent 24efc70 commit 8afe67c

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/balatrollm/balatrobench_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ def main() -> None:
4949
action="store_true",
5050
help="Convert PNG screenshots to AVIF format after analysis",
5151
)
52+
parser.add_argument(
53+
"--webp",
54+
action="store_true",
55+
help="Convert PNG screenshots to WebP format after analysis",
56+
)
5257

5358
args = parser.parse_args()
5459

@@ -80,6 +85,11 @@ def main() -> None:
8085
print("Converting PNG screenshots to AVIF format...")
8186
analyzer.convert_pngs_to_avif(args.output_dir)
8287

88+
# Convert PNGs to WebP if requested
89+
if args.webp:
90+
print("Converting PNG screenshots to WebP format...")
91+
analyzer.convert_pngs_to_webp(args.output_dir)
92+
8393
# Generate manifest.json in the base benchmark directory
8494
manifest_base_dir = args.output_dir.parent
8595
analyzer.generate_manifest(manifest_base_dir, __version__)

src/balatrollm/benchmark.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,50 @@ def convert_pngs_to_avif(self, directory: Path) -> None:
402402
except Exception as e:
403403
print(f"Warning: cavif conversion error in {directory}: {e}")
404404

405+
def _convert_single_png_to_webp(self, png_file: Path) -> None:
406+
"""Convert a single PNG file to WebP."""
407+
try:
408+
webp_file = png_file.with_suffix(".webp")
409+
subprocess.run(
410+
[
411+
"cwebp",
412+
"-q",
413+
"80",
414+
"-quiet",
415+
str(png_file),
416+
"-o",
417+
str(webp_file),
418+
],
419+
capture_output=True,
420+
text=True,
421+
check=True,
422+
)
423+
png_file.unlink()
424+
except subprocess.CalledProcessError as e:
425+
print(f"Warning: cwebp conversion failed for {png_file}: {e.stderr}")
426+
except OSError as e:
427+
print(f"Warning: Could not remove {png_file}: {e}")
428+
429+
def convert_pngs_to_webp(self, directory: Path) -> None:
430+
"""Convert all PNG files in directory to WebP using cwebp with parallelization."""
431+
try:
432+
png_files = list(directory.rglob("screenshot.png"))
433+
if not png_files:
434+
return
435+
436+
with ThreadPoolExecutor(max_workers=8) as executor:
437+
list(
438+
tqdm(
439+
executor.map(self._convert_single_png_to_webp, png_files),
440+
total=len(png_files),
441+
desc="Converting to WebP",
442+
)
443+
)
444+
except FileNotFoundError:
445+
print("Warning: cwebp not found, keeping PNG format")
446+
except Exception as e:
447+
print(f"Warning: cwebp conversion error in {directory}: {e}")
448+
405449
def extract_request_content(self, requests_file: Path) -> dict[str, str]:
406450
"""Extract request content from requests.jsonl by custom_id."""
407451
content_by_id = {}

0 commit comments

Comments
 (0)