Skip to content

Commit 413afca

Browse files
committed
Fix issues with saving and reading images w/ non-english filepaths
1 parent 6193f47 commit 413afca

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ max-args = 7
126126
# At least same as max-complexity
127127
max-branches = 15
128128

129+
[tool.ruff.lint.flake8-tidy-imports.banned-api]
130+
"cv2.imread".msg = """\
131+
it doesn't support special characters. Use `utils.imread` instead.
132+
https://github.com/opencv/opencv/issues/4292#issuecomment-2266019697"""
133+
"cv2.imwrite".msg = """\
134+
it doesn't support special characters. . Use `utils.imwrite` instead.
135+
https://github.com/opencv/opencv/issues/4292#issuecomment-2266019697"""
136+
129137
# https://github.com/hhatto/autopep8#usage
130138
# https://github.com/hhatto/autopep8#more-advanced-usage
131139
[tool.autopep8]

src/AutoSplit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
auto_split_directory,
6565
decimal,
6666
flatten,
67+
imwrite,
6768
is_valid_image,
6869
open_file,
6970
)
@@ -429,7 +430,7 @@ def __take_screenshot(self):
429430
return
430431

431432
# Save and open image
432-
cv2.imwrite(screenshot_path, capture)
433+
imwrite(screenshot_path, capture)
433434
if self.settings_dict["open_screenshot"]:
434435
open_file(screenshot_path)
435436

src/AutoSplitImage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import error_messages
1212
from compare import check_if_image_has_transparency, extract_and_compare_text, get_comparison_method_by_index
13-
from utils import BGR_CHANNEL_COUNT, MAXBYTE, TESSERACT_PATH, ColorChannel, ImageShape, is_valid_image
13+
from utils import BGR_CHANNEL_COUNT, MAXBYTE, TESSERACT_PATH, ColorChannel, ImageShape, imread, is_valid_image
1414

1515
if TYPE_CHECKING:
1616
from AutoSplit import AutoSplit
@@ -142,7 +142,7 @@ def __validate_ocr(self):
142142
)
143143

144144
def __read_image_bytes(self, path: str):
145-
image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
145+
image = imread(path, cv2.IMREAD_UNCHANGED)
146146
if not is_valid_image(image):
147147
self.byte_array = None
148148
error_messages.image_type(path)

src/region_selection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ImageShape,
1919
auto_split_directory,
2020
get_window_bounds,
21+
imread,
2122
is_valid_hwnd,
2223
is_valid_image,
2324
)
@@ -211,7 +212,7 @@ def align_region(autosplit: "AutoSplit"):
211212
if not template_filename:
212213
return
213214

214-
template = cv2.imread(template_filename, cv2.IMREAD_UNCHANGED)
215+
template = imread(template_filename, cv2.IMREAD_UNCHANGED)
215216
# Add alpha channel to template if it's missing.
216217
if template.shape[ImageShape.Channels] == BGR_CHANNEL_COUNT:
217218
template = cv2.cvtColor(template, cv2.COLOR_BGR2BGRA)

src/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import shutil
44
import subprocess # noqa: S404
55
import sys
6-
from collections.abc import Callable, Iterable
6+
from collections.abc import Callable, Iterable, Sequence
77
from enum import IntEnum
88
from functools import partial
99
from itertools import chain
1010
from platform import version
1111
from threading import Thread
1212
from typing import TYPE_CHECKING, Any, TypeAlias, TypedDict, TypeGuard, TypeVar
1313

14+
import cv2
15+
import numpy as np
1416
from cv2.typing import MatLike
1517

1618
from gen.build_vars import AUTOSPLIT_BUILD_NUMBER, AUTOSPLIT_GITHUB_REPOSITORY
@@ -260,6 +262,17 @@ def flatten(nested_iterable: Iterable[Iterable[T]]) -> chain[T]:
260262
return chain.from_iterable(nested_iterable)
261263

262264

265+
def imread(filename: str, flags: int = cv2.IMREAD_COLOR):
266+
return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), flags)
267+
268+
269+
def imwrite(filename: str, img: MatLike, params: Sequence[int] = ()):
270+
success, encoded_img = cv2.imencode(os.path.splitext(filename)[1], img, params)
271+
if not success:
272+
raise OSError(f"cv2 could not write to path {filename}")
273+
encoded_img.tofile(filename)
274+
275+
263276
def subprocess_kwargs():
264277
"""
265278
Create a set of arguments which make a ``subprocess.Popen`` (and

0 commit comments

Comments
 (0)