kwimage.im_demodata module¶
Keep a manifest of demo images used for testing
- kwimage.im_demodata.grab_test_image(key='astro', space='rgb', dsize=None, interpolation='lanczos')[source]¶
Ensures that the test image exists (this might use the network), reads it and returns the the image pixels.
- Parameters
key (str) – which test image to grab. Valid choices are: astro - an astronaught carl - Carl Sagan paraview - ParaView logo stars - picture of stars in the sky airport - SkySat image of Beijing Capital International Airport on 18 February 2018 See
kwimage.grab_test_image.keys
for a full list.space (str) – which colorspace to return in. Defaults to ‘rgb’
dsize (Tuple[int, int]) – if specified resizes image to this size
- Returns
the requested image
- Return type
ndarray
CommandLine
xdoctest -m kwimage.im_demodata grab_test_image
Example
>>> # xdoctest: +REQUIRES(--network) >>> import kwimage >>> for key in kwimage.grab_test_image.keys(): >>> print('attempt to grab key = {!r}'.format(key)) >>> kwimage.grab_test_image(key) >>> print('grabbed key = {!r}'.format(key)) >>> kwimage.grab_test_image('astro', dsize=(255, 255)).shape (255, 255, 3)
- kwimage.im_demodata.grab_test_image_fpath(key='astro', dsize=None, overviews=None)[source]¶
Ensures that the test image exists (this might use the network) and returns the cached filepath to the requested image.
- Parameters
key (str) – which test image to grab. Valid choices are: astro - an astronaught carl - Carl Sagan paraview - ParaView logo stars - picture of stars in the sky
dsize (None | Tuple[int, int]) – if specified, we will return a variant of the data with the specific dsize
overviews (None | int) – if specified, will return a variant of the data with overviews
- Returns
path to the requested image
- Return type
CommandLine
python -c "import kwimage; print(kwimage.grab_test_image_fpath('airport'))"
Example
>>> # xdoctest: +REQUIRES(--network) >>> import kwimage >>> for key in kwimage.grab_test_image.keys(): ... print('attempt to grab key = {!r}'.format(key)) ... kwimage.grab_test_image_fpath(key) ... print('grabbed grab key = {!r}'.format(key))
Example
>>> # xdoctest: +REQUIRES(--network) >>> import kwimage >>> key = ub.peek(kwimage.grab_test_image.keys()) >>> # specifying a dsize will construct a new image >>> fpath1 = kwimage.grab_test_image_fpath(key) >>> fpath2 = kwimage.grab_test_image_fpath(key, dsize=(32, 16)) >>> print('fpath1 = {}'.format(ub.repr2(fpath1, nl=1))) >>> print('fpath2 = {}'.format(ub.repr2(fpath2, nl=1))) >>> assert fpath1 != fpath2 >>> imdata2 = kwimage.imread(fpath2) >>> assert imdata2.shape[0:2] == (16, 32)
- kwimage.im_demodata.checkerboard(num_squares='auto', square_shape='auto', dsize=(512, 512), dtype=<class 'float'>, on_value=1, off_value=0)[source]¶
Creates a checkerboard image
- Parameters
num_squares (int | str) – Number of squares in a row. If ‘auto’ defaults to 8
square_shape (int | Tuple[int, int] | str) – If ‘auto’, chosen based on num_squares. Otherwise this is the height, width of each square in pixels.
dsize (Tuple[int, int]) – width and height
dtype (type) – return data type
on_value (Number) – The value of one checker. Defaults to 1.
off_value (Number) – The value off the other checker. Defaults to 0.
References
https://stackoverflow.com/questions/2169478/how-to-make-a-checkerboard-in-numpy
Example
>>> import kwimage >>> import numpy as np >>> img = kwimage.checkerboard() >>> print(kwimage.checkerboard(dsize=(16, 16)).shape) >>> print(kwimage.checkerboard(num_squares=4, dsize=(16, 16)).shape) >>> print(kwimage.checkerboard(square_shape=3, dsize=(23, 17)).shape) >>> print(kwimage.checkerboard(square_shape=3, dsize=(1451, 1163)).shape) >>> print(kwimage.checkerboard(square_shape=3, dsize=(1202, 956)).shape) >>> print(kwimage.checkerboard(dsize=(4, 4), on_value=(255, 0, 0), off_value=(0, 0, 1), dtype=np.uint8))
Example
>>> import kwimage >>> img = kwimage.checkerboard( >>> dsize=(64, 64), on_value='kw_green', off_value='kw_blue') >>> # xdoc: +REQUIRES(--show) >>> # xdoc: +REQUIRES(module:kwplot) >>> import kwplot >>> kwplot.autoplt() >>> kwplot.imshow(img) >>> kwplot.show_if_requested()