Skip to content

Commit

Permalink
Add tests to make sure transparent files are well handled (#167)
Browse files Browse the repository at this point in the history
* Add new transparent test image
  • Loading branch information
leblancfg authored Mar 14, 2022
1 parent 6b45d33 commit a388919
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
9 changes: 6 additions & 3 deletions autocrop/autocrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ def distance(pt1, pt2):

def bgr_to_rbg(img):
"""Given a BGR (cv2) numpy array, returns a RBG (standard) array."""
dimensions = len(img.shape)
if dimensions == 2:
# Don't do anything for grayscale images
if img.ndim == 2:
return img
return img[..., ::-1]

# Flip the channels. Use explicit indexing in case RGBA is used.
img[:, :, [0, 1, 2]] = img[:, :, [2, 1, 0]]
return img


def gamma(img, correction):
Expand Down
Binary file added tests/data/expo_67.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 16 additions & 4 deletions tests/test_autocrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from glob import glob
import shutil

import pytest # noqa: F401
import cv2
import numpy as np
Expand Down Expand Up @@ -79,16 +80,16 @@ def test_adjust_boundaries(values, expected_result):
@pytest.mark.slow
@pytest.mark.parametrize(
"height, width",
[(500, 500), (900, 500), (500, 900), (1000, 1200)],
[(500, 500), (700, 500), (500, 700), (1000, 1200)],
)
def test_detect_face_in_cropped_image(height, width, integration):
"""An image cropped by Cropper should have a face detectable.
"""
An image cropped by Cropper should have a face detectable.
This defends us against image warping or crops outside the region
of interest.
"""
c = Cropper(height=height, width=width)
c = Cropper(height=height, width=width, face_percent=1, resize=False)
faces = [f for f in glob("tests/test/*") if not f.endswith("md")]
print(faces)
for face in faces:
try:
img_array = c.crop(face)
Expand Down Expand Up @@ -119,3 +120,14 @@ def test_face_percent(face_percent):
with pytest.raises(ValueError) as e:
Cropper(face_percent=face_percent)
assert "argument must be between 0 and 1" in str(e)


def test_transparent_png(integration):
c = Cropper()
img = c.crop("tests/test/expo_67.png")

# Make sure we're still RGBA
assert img.shape[-1] == 4

# Make sure the first pixel is transparent
assert img[0, 0, 3] == 0
12 changes: 7 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from autocrop.autocrop import Cropper
from autocrop.cli import command_line_interface, main, size, confirmation, chk_extension

NUM_FILES = 12


@pytest.fixture()
def integration():
Expand Down Expand Up @@ -160,7 +162,7 @@ def test_main_overwrites_when_same_input_and_output(integration):
sys.argv = ["", "--no-confirm", "-i", "tests/test", "-o", "tests/test"]
command_line_interface()
output_files = os.listdir(sys.argv[-1])
assert len(output_files) == 11
assert len(output_files) == NUM_FILES


# @mock.patch("autocrop.autocrop.Cropper", side_)
Expand All @@ -176,7 +178,7 @@ def crop(self, *args):
m = MonkeyCrop()
monkeypatch.setattr(Cropper, "crop", m.crop)
main("tests/test", None, None, None)
assert m.count == 10
assert m.count == NUM_FILES - 1


@mock.patch("autocrop.cli.main", lambda *args: None)
Expand Down Expand Up @@ -212,7 +214,7 @@ def test_noface_files_copied_over_if_output_d_specified(integration):
sys.argv = ["", "-i", "tests/test", "-o", "tests/crop"]
command_line_interface()
output_files = os.listdir(sys.argv[-1])
assert len(output_files) == 10
assert len(output_files) == NUM_FILES - 1


@pytest.mark.slow
Expand All @@ -229,7 +231,7 @@ def test_nofaces_copied_to_reject_d_if_both_reject_and_output_d(integration):
command_line_interface()
output_files = os.listdir(sys.argv[-3])
reject_files = os.listdir(sys.argv[-1])
assert len(output_files) == 7
assert len(output_files) == NUM_FILES - 4
assert len(reject_files) == 3


Expand All @@ -240,7 +242,7 @@ def test_image_files_overwritten_if_no_output_dir(integration):
command_line_interface()
# We have the same number of files
output_files = os.listdir(sys.argv[-1])
assert len(output_files) == 11
assert len(output_files) == NUM_FILES
# Images with a face have been cropped
shape = cv2.imread("tests/test/king.jpg").shape
assert shape == (500, 500, 3)
Expand Down

0 comments on commit a388919

Please sign in to comment.