Skip to content

Commit 06726f1

Browse files
committed
Refactor set_out_file() logic
1 parent 3436673 commit 06726f1

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

batch_img/border.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ def border_1_image(
4040
bd_img = Image.new(img.mode, (width, height), bd_color)
4141
bd_img.paste(cropped_img, (bd_width, bd_width))
4242

43-
if out_path == REPLACE:
44-
file = Path(f"{in_path.parent}/{in_path.stem}_tmp{in_path.suffix}")
45-
else:
46-
file = Common.set_out_file(in_path, out_path, f"bw{bd_width}")
43+
file = Common.set_out_file(in_path, out_path, f"bw{bd_width}")
4744
if "exif" in img.info:
4845
exif_dict = piexif.load(img.info["exif"])
4946
exif_bytes = piexif.dump(exif_dict)
@@ -53,7 +50,7 @@ def border_1_image(
5350
logger.info(f"Saved image with border to {file}")
5451
if out_path == REPLACE:
5552
os.replace(file, in_path)
56-
logger.info(f"Replaced {in_path} with tmp_file")
53+
logger.info(f"Replaced {in_path} with the new tmp_file")
5754
file = in_path
5855
return True, file
5956
except (AttributeError, FileNotFoundError, ValueError) as e:

batch_img/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ def set_out_file(in_path: Path, out_path: Path, extra: str) -> Path:
334334
Returns:
335335
Path:
336336
"""
337+
if out_path == REPLACE:
338+
out_file = Path(f"{in_path.parent}/{in_path.stem}_tmp{in_path.suffix}")
339+
return out_file
337340
out_path.mkdir(parents=True, exist_ok=True)
338341
out_file = out_path
339342
if out_path.is_dir():

batch_img/resize.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ def resize_an_image(in_path: Path, out_path: Path | str, length: int) -> tuple:
3434
max_size = (length, length)
3535
# The thumbnail() keeps the original aspect ratio
3636
img.thumbnail(max_size, Image.Resampling.LANCZOS)
37-
if out_path == REPLACE:
38-
file = Path(f"{in_path.parent}/{in_path.stem}_tmp{in_path.suffix}")
39-
else:
40-
file = Common.set_out_file(in_path, out_path, f"{length}")
4137

38+
file = Common.set_out_file(in_path, out_path, f"{length}")
4239
if "exif" in img.info:
4340
exif_dict = piexif.load(img.info["exif"])
4441
exif_bytes = piexif.dump(exif_dict)
@@ -48,7 +45,7 @@ def resize_an_image(in_path: Path, out_path: Path | str, length: int) -> tuple:
4845
logger.info(f"Saved resized image to {file}")
4946
if out_path == REPLACE:
5047
os.replace(file, in_path)
51-
logger.info(f"Replaced {in_path} with tmp_file")
48+
logger.info(f"Replaced {in_path} with the new tmp_file")
5249
file = in_path
5350
return True, file
5451
except (AttributeError, FileNotFoundError, ValueError) as e:

batch_img/rotate.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ def rotate_1_image(in_path: Path, out_path: Path | str, angle_cw: int) -> tuple:
3939
exif_dict["0th"][piexif.ImageIFD.Orientation] = 1
4040
exif_bytes = piexif.dump(exif_dict)
4141

42-
if out_path == REPLACE:
43-
file = Path(f"{in_path.parent}/{in_path.stem}_tmp{in_path.suffix}")
44-
else:
45-
file = Common.set_out_file(in_path, out_path, f"{angle_cw}cw")
42+
file = Common.set_out_file(in_path, out_path, f"{angle_cw}cw")
4643
# img.rotate() for any angle (slower & slight quality loss)
4744
rotated_img = img
4845
if angle_cw == 90:
@@ -56,7 +53,7 @@ def rotate_1_image(in_path: Path, out_path: Path | str, angle_cw: int) -> tuple:
5653
logger.info(f"Saved ({angle_cw}°) clockwise rotated to {file}")
5754
if out_path == REPLACE:
5855
os.replace(file, in_path)
59-
logger.info(f"Replaced {in_path} with tmp_file")
56+
logger.info(f"Replaced {in_path} with the new tmp_file")
6057
file = in_path
6158
return True, file
6259
except (AttributeError, FileNotFoundError, ValueError) as e:

tests/test_common.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import requests
1313

1414
from batch_img.common import Common
15-
from batch_img.const import NAME, PKG_NAME, VER
15+
from batch_img.const import PKG_NAME, REPLACE
1616
from .helper import DotDict
1717

1818

@@ -299,3 +299,29 @@ def test_get_crop_box(data_get_crop_box):
299299
width, height, border_width, expected = data_get_crop_box
300300
actual = Common.get_crop_box(width, height, border_width)
301301
assert actual == expected
302+
303+
304+
@pytest.fixture(
305+
params=[
306+
(
307+
Path(f"{dirname(__file__)}/data/HEIC/Cartoon.heic"),
308+
Path(f"{dirname(__file__)}/.out/"),
309+
"90cw",
310+
Path(f"{dirname(__file__)}/.out/Cartoon_90cw.heic"),
311+
),
312+
(
313+
Path(f"{dirname(__file__)}/data/HEIC/Cartoon.heic"),
314+
REPLACE,
315+
"90cw",
316+
Path(f"{dirname(__file__)}/data/HEIC/Cartoon_tmp.heic"),
317+
),
318+
]
319+
)
320+
def data_set_out_file(request):
321+
return request.param
322+
323+
324+
def test_set_out_file(data_set_out_file):
325+
in_path, out_path, extra, expected = data_set_out_file
326+
actual = Common.set_out_file(in_path, out_path, extra)
327+
assert actual == expected

0 commit comments

Comments
 (0)