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
33 changes: 33 additions & 0 deletions AutoTransformPy/translate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from skimage.io import imread, imshow
from skimage.transform import AffineTransform, warp
import numpy as np

def translate (image_path, num_images, max_translation):
"""Returns an array of images of length num_images randomly translated a random number of pixels up to max_rotation

Expand All @@ -18,3 +22,32 @@ def translate (image_path, num_images, max_translation):
--------
translated_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_translation, int):
raise TypeError("The maximum translation 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.")

org_image = imread(image_path)

if max_translation >= org_image.shape[0] or max_translation >= org_image.shape[1]:
raise ValueError("The maximum translation must be less than the width and height of the image.")

# Perform image translation
translations = (np.random.randint(-max_translation, max_translation, num_images), np.random.randint(-max_translation, max_translation, num_images))
translated_images = [org_image]

for i in range(len(translations[0])):
tform = AffineTransform(translation=(translations[0][i], translations[1][i]))
translated_images.append(warp(org_image, tform, mode="constant", preserve_range=True).astype('uint8'))

return np.asarray(translated_images)
Binary file added tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
26 changes: 15 additions & 11 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import sys
sys.path.append("../AutoTransformPy")
import AutoTransformPy as pre
import pytest
from skimage.io import imread
sys.path.append("../AutoTransformPy/")
import translate as trans

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

with pytest.raises(ValueError):
pre.translate("PathTo300x300Image", 7, 301) # Outside of the translation range, possible to get empty images
trans.translate("../tests/imgs/milad.jpg", 7, 1000) # Outside of the translation range, possible to get empty images

def return_imgs(): # Tests that the number of images returned from translate is correct
test_img = skimage.io.imread("test_img")
returned_arr = pre.translate("test_image", 5, 10)
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 translate is correct
test_img = imread("../tests/imgs/milad.jpg")
returned_arr = trans.translate("../tests/imgs/milad.jpg", 5, 10)

assert returned_arr[0].shape == test_img.shape
assert returned_arr.shape[0] == 6
assert returned_arr.shape[1:] == test_img.shape