|
15 | 15 | from typing import Union, Tuple, List, Dict, Any, Iterator
|
16 | 16 | from abc import abstractmethod
|
17 | 17 | from pathlib import Path
|
| 18 | +import mimetypes |
18 | 19 | import json
|
19 | 20 | import copy
|
20 | 21 | import numpy as np
|
@@ -184,10 +185,16 @@ def save_to_json(
|
184 | 185 | *args: Additional positional arguments to pass to the underlying writer.
|
185 | 186 | **kwargs: Additional keyword arguments to pass to the underlying writer.
|
186 | 187 | """
|
187 |
| - if not str(save_path).endswith(".json"): |
| 188 | + |
| 189 | + def _is_json_file(file_path): |
| 190 | + mime_type, _ = mimetypes.guess_type(file_path) |
| 191 | + return mime_type is not None and mime_type == "application/json" |
| 192 | + |
| 193 | + if not _is_json_file(save_path): |
188 | 194 | save_path = Path(save_path) / f"{Path(self['input_path']).stem}.json"
|
| 195 | + save_path = save_path.as_posix() |
189 | 196 | self._json_writer.write(
|
190 |
| - save_path.as_posix(), |
| 197 | + save_path, |
191 | 198 | self.json,
|
192 | 199 | indent=indent,
|
193 | 200 | ensure_ascii=ensure_ascii,
|
@@ -288,10 +295,16 @@ def save_to_img(self, save_path: str, *args: List, **kwargs: Dict) -> None:
|
288 | 295 | *args: Additional positional arguments that will be passed to the image writer.
|
289 | 296 | **kwargs: Additional keyword arguments that will be passed to the image writer.
|
290 | 297 | """
|
291 |
| - if not str(save_path).lower().endswith((".jpg", ".png")): |
| 298 | + |
| 299 | + def _is_image_file(file_path): |
| 300 | + mime_type, _ = mimetypes.guess_type(file_path) |
| 301 | + return mime_type is not None and mime_type.startswith("image/") |
| 302 | + |
| 303 | + if not _is_image_file(save_path): |
292 | 304 | fp = Path(self["input_path"])
|
293 | 305 | save_path = Path(save_path) / f"{fp.stem}{fp.suffix}"
|
294 |
| - self._img_writer.write(save_path.as_posix(), self.img, *args, **kwargs) |
| 306 | + save_path = save_path.as_posix() |
| 307 | + self._img_writer.write(save_path, self.img, *args, **kwargs) |
295 | 308 |
|
296 | 309 |
|
297 | 310 | class CSVMixin:
|
|
0 commit comments