Skip to content

Commit 22f8bcd

Browse files
committed
[scene_manager] Fix save_images not working with UTF-8 paths #450
1 parent e1d86d9 commit 22f8bcd

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

scenedetect/scene_manager.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def on_new_scene(frame_img: numpy.ndarray, frame_num: int):
8686
import sys
8787
import threading
8888
from enum import Enum
89+
from pathlib import Path
8990
from typing import Callable, Dict, Iterable, List, Optional, TextIO, Tuple, Union
9091

9192
import cv2
@@ -587,8 +588,13 @@ def save_images(
587588
frame_im = cv2.resize(
588589
frame_im, (0, 0), fx=scale, fy=scale, interpolation=interpolation.value
589590
)
590-
591-
cv2.imwrite(get_and_create_path(file_path, output_dir), frame_im, imwrite_param)
591+
path = Path(get_and_create_path(file_path, output_dir))
592+
(is_ok, encoded) = cv2.imencode(f".{image_extension}", frame_im, imwrite_param)
593+
if is_ok:
594+
encoded.tofile(path)
595+
else:
596+
logger.error(f"Failed to encode image for {file_path}")
597+
#
592598
else:
593599
completed = False
594600
break

tests/test_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pathlib import Path
1818

1919
import cv2
20+
import numpy as np
2021
import pytest
2122

2223
from scenedetect.video_splitter import is_ffmpeg_available, is_mkvmerge_available
@@ -464,6 +465,23 @@ def test_cli_save_images(tmp_path: Path):
464465
assert image.shape == (544, 1280, 3)
465466

466467

468+
def test_cli_save_images_path_handling(tmp_path: Path):
469+
"""Test `save-images` ability to handle UTF-8 paths."""
470+
assert (
471+
invoke_scenedetect(
472+
"-i {VIDEO} -s {STATS} time {TIME} {DETECTOR} save-images -f %s"
473+
% ("電腦檔案-$SCENE_NUMBER-$IMAGE_NUMBER"),
474+
output_dir=tmp_path,
475+
)
476+
== 0
477+
)
478+
# Check the created images can be read and have the correct size.
479+
images = [image for image in glob.glob("電腦檔案-*.jpg")]
480+
assert images
481+
image = cv2.imdecode(np.fromfile(images[0], dtype=np.uint8), cv2.IMREAD_UNCHANGED)
482+
assert image.shape == (544, 1280, 3)
483+
484+
467485
# TODO(#134): This works fine with OpenCV currently, but needs to be supported for PyAV and MoviePy.
468486
def test_cli_save_images_rotation(rotated_video_file, tmp_path):
469487
"""Test that `save-images` command rotates images correctly with the default backend."""

website/pages/changelog.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,19 +583,20 @@ Development
583583

584584
## PySceneDetect 0.6.5 (TBD)
585585

586-
- [bugfix] Fix new detectors not working with `default-detector` config option
587-
- [bugfix] Fix crash when using `save-images`/`save_images()` with OpenCV backend [#455](https://github.com/Breakthrough/PySceneDetect/issues/455)
586+
- [bugfix] Fix `SyntaxWarning` due to incorrect escaping [#400](https://github.com/Breakthrough/PySceneDetect/issues/400)
587+
- [bugfix] Fix `ContentDetector` crash when using callbacks [#416](https://github.com/Breakthrough/PySceneDetect/issues/416) [#420](https://github.com/Breakthrough/PySceneDetect/issues/420)
588+
- [feature] Add ability to configure CSV separators for rows/columns in config file [#423](https://github.com/Breakthrough/PySceneDetect/issues/423)
589+
- [feature] Add new `--show` flag to `export-html` command to launch browser after processing [#442](https://github.com/Breakthrough/PySceneDetect/issues/442)
588590
- [general] Timecodes of the form `MM:SS[.nnn]` are now processed correctly [#443](https://github.com/Breakthrough/PySceneDetect/issues/443)
589-
- [feature] Add new `--show` flag to `export-html` command to launch browser after processing (#442)
591+
- [bugfix] Fix `save-images`/`save_images()` not working correctly with UTF-8 paths [#450](https://github.com/Breakthrough/PySceneDetect/issues/455)
592+
- [bugfix] Fix crash when using `save-images`/`save_images()` with OpenCV backend [#455](https://github.com/Breakthrough/PySceneDetect/issues/455)
593+
- [bugfix] Fix new detectors not working with `default-detector` config option
590594
- [improvement] The `export-html` command now implicitly invokes `save-images` with default parameters
591595
- The output of the `export-html` command will always use the result of the `save-images` command that *precedes* it
592596
- [general] Updates to Windows distributions:
593597
- The MoviePy backend is now included with Windows distributions
594598
- Bundled Python interpreter is now Python 3.13
595599
- Updated PyAV 10 -> 13.1.0 and OpenCV 4.10.0.82 -> 4.10.0.84
596600
- [improvement] `save_to_csv` now works with paths from `pathlib`
597-
- [bugfix] Fix `SyntaxWarning` due to incorrect escaping [#400](https://github.com/Breakthrough/PySceneDetect/issues/400)
598-
- [bugfix] Fix `ContentDetector` crash when using callbacks [#416](https://github.com/Breakthrough/PySceneDetect/issues/416) [#420](https://github.com/Breakthrough/PySceneDetect/issues/420)
599601
- [api] The `save_to_csv` function now works correctly with paths from the `pathlib` module
600602
- [api] Add `col_separator` and `row_separator` args to `write_scene_list` function in `scenedetect.scene_manager`
601-
- [feature] Add ability to configure CSV separators for rows/columns in config file [#423](https://github.com/Breakthrough/PySceneDetect/issues/423)

0 commit comments

Comments
 (0)