Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified AutoTransformPy/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
31 changes: 31 additions & 0 deletions AutoTransformPy/rotate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from skimage.io import imread
from skimage.transform import rotate as rot
import numpy as np

def rotate (image_path, num_images, max_rotation):
"""Returns an array of images of length num_images randomly rotated a random degree up to max_rotation

Expand All @@ -19,3 +23,30 @@ def rotate (image_path, num_images, max_rotation):
--------
rotated_images: np.array
"""

# Check for valid input paramters types
if not isinstance(image_path, str):
raise TypeError("The file path must be a string.")

if not isinstance(num_images, int):
raise TypeError("The number of images returned must be an integer.")

if not isinstance(max_rotation, int):
raise TypeError("The maximum rotation of images must be an integer.")

# Check for valid input paramter values
if num_images < 1:
raise ValueError("The number of images returned must be 1 or greater.")

if max_rotation < 1 or max_rotation > 360:
raise ValueError("The maximum rotation must be between 1 and 360.")

# Perform image rotation
rotations = np.random.randint(-max_rotation, max_rotation, num_images)
org_image = imread(image_path)
rotated_images = []

for a_rotation in rotations:
rotated_images.append(rot(org_image, a_rotation, resize=False))

return np.asarray(rotated_images)
Binary file added tests/imgs/milad.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 14 additions & 11 deletions tests/test_rotate.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import sys
sys.path.append("../AutoTransformPy")
import AutoTransformPy as pre
import pytest
from skimage.io import imread
sys.path.append("../AutoTransformPy/")
import rotate as rot

def test_inputs():
with pytest.raises(TypeError):
pre.rotate(6, 5, 345) # Not a string for the file path
pre.rotate("Path", "seven", 345) # Not a valid number of images
pre.rotate("Path", 6, "eight") # Not a valid rotation amount
rot.rotate(6, 5, 345) # Not a string for the file path
rot.rotate("../tests/imgs/milad.jpg", "seven", 345) # Not a valid number of images
rot.rotate("../tests/imgs/milad.jpg", 6, "eight") # Not a valid rotation amount

with pytest.raises(ValueError):
pre.rotate("Path", 7, 500) # Outside of the rotation range
rot.rotate("../tests/imgs/milad.jpg", 7, 500) # Outside of the rotation range
rot.rotate("../tests/imgs/milad.jpg", -1, 500) # Nonsense number of images to return

def return_imgs(): # Tests that the number of images returned from translate is correct
test_img = skimage.io.imread("test_img")
returned_arr = pre.rotate("test_image", 5, 180)
assert returned_arr.shape()[0] == 5
assert returned_arr.shape()[1:] == test_img.shape()
def test_return_imgs(): # Tests that the number of images returned from rotate is correct
test_img = imread("../tests/imgs/milad.jpg")
returned_arr = rot.rotate("../tests/imgs/milad.jpg", 5, 180)
assert returned_arr.shape[0] == 5
assert returned_arr.shape[1:] == test_img.shape