From e02f0b7f4432cd69a83412bfc69f0a39c195f500 Mon Sep 17 00:00:00 2001 From: aswanth-07 Date: Sat, 21 Nov 2026 17:00:07 +0530 Subject: [PATCH 1/2] [bugfix] Preserve aspect ratio when exported frames change size --- scenedetect/output/image.py | 27 +----------------------- tests/test_output.py | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/scenedetect/output/image.py b/scenedetect/output/image.py index b4bdbbb2..87654cf6 100644 --- a/scenedetect/output/image.py +++ b/scenedetect/output/image.py @@ -586,32 +586,7 @@ def save_images( image_extension, ) image_filenames[i].append(file_path) - # TODO: Combine this resize with the ones below. - if aspect_ratio is not None: - frame_im = cv2.resize( - frame_im, (0, 0), fx=aspect_ratio, fy=1.0, interpolation=interpolation.value - ) - frame_height = frame_im.shape[0] - frame_width = frame_im.shape[1] - - # Figure out what kind of resizing needs to be done - if height or width: - if height and not width: - factor = height / float(frame_height) - width = int(factor * frame_width) - elif width and not height: - factor = width / float(frame_width) - height = int(factor * frame_height) - assert height is not None - assert width is not None - assert height > 0 and width > 0 - frame_im = cv2.resize( - frame_im, (width, height), interpolation=interpolation.value - ) - elif scale: - frame_im = cv2.resize( - frame_im, (0, 0), fx=scale, fy=scale, interpolation=interpolation.value - ) + frame_im = _scale_image(frame_im, aspect_ratio, height, width, scale, interpolation) path = Path(get_and_create_path(file_path, output_dir)) (is_ok, encoded) = cv2.imencode(f".{image_extension}", frame_im, imwrite_param) if is_ok: diff --git a/tests/test_output.py b/tests/test_output.py index 4c4ee220..a8d1c01d 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -18,8 +18,11 @@ from fractions import Fraction from io import StringIO from pathlib import Path +from unittest.mock import Mock from xml.etree import ElementTree +import cv2 +import numpy as np import pytest from scenedetect import ( @@ -44,6 +47,7 @@ write_scene_list_ffmetadata, write_scene_list_otio, ) +from scenedetect.video_stream import VideoStream FFMPEG_ARGS = ( "-vf crop=128:128:0:0 -map 0:v:0 -c:v libx264 -preset ultrafast -qp 0 -tune zerolatency" @@ -179,6 +183,43 @@ def test_save_images_singlethreaded(test_video_file, tmp_path: Path): assert total_images == len([path for path in tmp_path.glob(image_name_glob)]) +@pytest.mark.parametrize("threading", [False, True]) +@pytest.mark.parametrize( + "resize, expected_shapes", + [ + ({"width": 80}, [(40, 80), (60, 80)]), + ({"height": 60}, [(60, 120), (60, 80)]), + ], +) +def test_save_images_resizes_each_frame(tmp_path: Path, threading, resize, expected_shapes): + video = Mock(spec=VideoStream) + video.name = "variable-resolution" + video.aspect_ratio = 1.0 + video.read.side_effect = [ + np.zeros((80, 160, 3), dtype=np.uint8), + np.zeros((120, 160, 3), dtype=np.uint8), + ] + scenes = [(FrameTimecode(0, 10.0), FrameTimecode(2, 10.0))] + + filenames = save_images( + scenes, + video, + num_images=2, + image_extension="png", + output_dir=tmp_path, + threading=threading, + **resize, + ) + + shapes = [ + cv2.imdecode(np.fromfile(tmp_path / filename, dtype=np.uint8), cv2.IMREAD_UNCHANGED).shape[ + :2 + ] + for filename in filenames[0] + ] + assert shapes == expected_shapes + + @pytest.mark.parametrize("frame_margin", [1, 0.1, "0.1s", "00:00:00.100"]) def test_save_images_frame_margin_accepts_time_values( test_video_file, tmp_path: Path, frame_margin From cdf6a44de273c3c40a3c36a6eaf9e7953c020c15 Mon Sep 17 00:00:00 2001 From: aswanth-07 Date: Sun, 22 Nov 2026 10:59:19 +0530 Subject: [PATCH 2/2] [bugfix] Document frame resizing and verify image decoding --- tests/test_output.py | 12 ++++++------ website/pages/changelog.md | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/test_output.py b/tests/test_output.py index a8d1c01d..697649e5 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -211,12 +211,12 @@ def test_save_images_resizes_each_frame(tmp_path: Path, threading, resize, expec **resize, ) - shapes = [ - cv2.imdecode(np.fromfile(tmp_path / filename, dtype=np.uint8), cv2.IMREAD_UNCHANGED).shape[ - :2 - ] - for filename in filenames[0] - ] + shapes = [] + for filename in filenames[0]: + data = np.fromfile(tmp_path / filename, dtype=np.uint8) + image = cv2.imdecode(data, cv2.IMREAD_UNCHANGED) + assert image is not None, f"Could not decode {filename}" + shapes.append(image.shape[:2]) assert shapes == expected_shapes diff --git a/website/pages/changelog.md b/website/pages/changelog.md index f01a9272..113c22da 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -864,6 +864,7 @@ Development - [bugfix] The CLI now exits with 1 when no frames could be read from the input video, or seeking to the `time -s`/`--start` time fails, instead of 0 - [bugfix] `--kernel-size`/`-k` values the detectors cannot use (even numbers, 1, 0 and negatives other than -1) now give a usage error instead of a traceback, and `-k -1` now means auto, like the config file. A config file with `kernel-size = 1` is now an error [#604](https://github.com/Breakthrough/PySceneDetect/issues/604) - [bugfix] `save-images` no longer takes the last image of a scene from the first frame of the next scene. `--frame-margin` is counted from the last frame of the scene again, as in v0.6 (`-m 0` now selects the last frame itself) + - [bugfix] Single-threaded `save_images()` preserves each frame's aspect ratio when the video changes resolution and only a width or height is specified [#615](https://github.com/Breakthrough/PySceneDetect/pull/615) - [bugfix] Out of range values for `detect-adaptive --frame-window` (below 1) and `save-images` `--num-images` (below 1), `--scale`, `--height` and `--width` (negative) now give a usage error instead of a traceback, and the same values are rejected in a config file [#609](https://github.com/Breakthrough/PySceneDetect/issues/609) - [bugfix] `detect-content` / `detect-adaptive` `--weights` set to all zeros, or with a negative or non-finite weight (also in a config file), now give an error instead of NaN frame scores and no detected scenes, and `ContentDetector` / `AdaptiveDetector` raise a `ValueError` for them [#612](https://github.com/Breakthrough/PySceneDetect/issues/612) - [bugfix] `FrameTimecode.get_timecode()` with `use_rounding=False` now truncates instead of sometimes rounding up the last digit. Previously 0.4996 seconds with `precision=2` gave `00:00:00.50`, and 1.99997 seconds gave `00:00:01.000` instead of `00:00:01.999`. [#611](https://github.com/Breakthrough/PySceneDetect/issues/611)