Source code for imagepypelines_image.data
import os
import glob
import sys
from types import FunctionType, SimpleNamespace
import numpy as np
from functools import partial
from .imports import import_opencv
cv2 = import_opencv()
# ------------------------------ Standard Imagery ------------------------------
[docs]def standard_image_filenames():
"""returns a list of standard image filenames on the local machine"""
sorted_keys = list_standard_images()
filenames = [STANDARD_IMAGES[k] for k in sorted_keys]
return filenames
[docs]def standard_image_gen():
"""
generator function to yield all standard images sequentially
useful for testing
"""
for img_name in STANDARD_IMAGES.values()():
yield get_standard_image(img_name)
[docs]def standard_images():
"""returns a list of all standard image arrays"""
return list( standard_image_gen() )
[docs]def get_standard_image(img_name):
""" retrieves the numpy array of standard image given a string key
Args:
img_name (str): name of the standard image to retrieve, must be in
list_standard_images()
Returns:
np.ndarray: image data for the given standard image
Raises:
ValueError: if invalid img_name is provided
Example:
>>> import imagepypelines as ip
>>> lenna = ip.get_standard_image('lenna')
"""
if img_name in STANDARD_IMAGES:
img = cv2.imread(STANDARD_IMAGES[img_name], cv2.IMREAD_UNCHANGED)
if isinstance(img, type(None)):
error_msg = "unable to find {name} at {path}".format(name=img_name,
path=STANDARD_IMAGES[img_name])
raise FileNotFoundError(error_msg)
return img
else:
raise ValueError("unknown standard image key {img_name}, must be \
one of {std_imgs}".format(
img_name=img_name,
std_imgs=STANDARD_IMAGES.keys()))
# uses the pkg_resources provider to load in data in the .egg file
from . import STANDARD_IMAGE_DIRECTORY
# ND 9/7/18 - dynamically populate paths to the standard test images
# assumes the only thing in the STANDARD_IMAGE_DIRECTORY are images
STANDARD_IMAGE_PATHS = list(
glob.glob(os.path.join(STANDARD_IMAGE_DIRECTORY, '*')))
STANDARD_IMAGES = {os.path.basename(impath).split(
'.')[0]: impath for impath in STANDARD_IMAGE_PATHS}
# ND 9/7/18 - create convenience functions to load each of the standard test
# images as attributes of func
funcs = SimpleNamespace()
for img_name in STANDARD_IMAGES.keys():
# JM: modifies function creation to also include docstrings
partial_func = partial(get_standard_image, img_name)
# ND changed partial funcs to FunctionType
std_img_func = FunctionType(
partial_func.func.__code__, globals(), img_name, partial_func.args)
std_img_func.__doc__ = "standard image retrieval for {}".format(img_name)
globals()[img_name] = std_img_func
setattr(funcs, img_name, std_img_func)
# JM: deletes last remaining partial function from scope to remove Sphinx warning
del partial_func, img_name