util¶
Common utilities for the pjimg
packages.
convert¶
Data conversion utilities.
- pjimg.util.float_to_uint8(a: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) ndarray[tuple[int, ...], dtype[uint8]] [source]¶
Convert an array of floating point values to an array of unsigned 8-bit integers.
- Parameters:
a – The array of image data to convert to unsigned 8-bit integers.
- Returns:
A
numpy.ndarray
object.- Return type:
lerps¶
Interpolation functions.
- pjimg.util.cubic_interpolation(a: ndarray[tuple[int, ...], dtype[T]], b: ndarray[tuple[int, ...], dtype[T]], x: ndarray[tuple[int, ...], dtype[float64]], a_: ndarray[tuple[int, ...], dtype[T]] | None = None, b_: ndarray[tuple[int, ...], dtype[T]] | None = None) ndarray[tuple[int, ...], dtype[T]] [source]¶
Perform a cubic interpolation on the values of four arrays. This is adapted from code found at: Cubic Interpolation
- Parameters:
a – The closest value on the “left” side.
b – The closest value on the “right” side.
x – How close the final value is to the closest “left” value.
a – (Optional.) The farther value on the “left” side.
b – (Optional.) The farther value on the “right” side.
- Returns:
A
numpy.ndarray
object.- Return type:
Usage:
>>> import numpy as np >>> >>> base = np.array([0, 1, 2, 3, 4, 5], dtype=float) >>> a = base[1:4] >>> b = base[2:5] >>> a_ = base[:3] >>> b_ = base[3:] >>> x = np.array([0.5, 0.5, 0.5]) >>> cubic_interpolation(a, b, x, a_, b_) array([1.5, 2.5, 3.5])
You can do the interpolation if you don’t have the furthest data points. It will just be inaccurate at the edges because of the missing data:
>>> base = np.array([1, 2, 3, 4], dtype=float) >>> a = base[:3] >>> b = base[1:] >>> x = np.array([0.5, 0.5, 0.5]) >>> cubic_interpolation(a, b, x) array([1.4375, 2.5 , 3.5625])
- pjimg.util.linear_interpolation(a: ndarray[tuple[int, ...], dtype[T]], b: ndarray[tuple[int, ...], dtype[T]], x: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[T]] [source]¶
Perform a linear interpolation on the values of two arrays
- Parameters:
a – The “left” values. The datatype of a also determines the datatype of the returned array.
b – The “right” values.
x – An array of how close the location of the final value should be to the “left” value.
- Returns:
A
numpy.ndarray
object.- Return type:
Usage:
>>> import numpy as np >>> >>> a = np.array([1, 2, 3]) >>> b = np.array([3, 4, 5]) >>> x = np.array([.5, .5, .5]) >>> linear_interpolation(a, b, x) array([2, 3, 4])
- pjimg.util.n_dimensional_interpolation(a: ndarray[tuple[int, ...], dtype[T]], b: ndarray[tuple[int, ...], dtype[T]], x: ndarray[tuple[int, ...], dtype[float64]], interpolator: Callable[[ndarray[tuple[int, ...], dtype[T]], ndarray[tuple[int, ...], dtype[T]], ndarray[tuple[int, ...], dtype[float64]]], ndarray[tuple[int, ...], dtype[T]]]) ndarray[tuple[int, ...], dtype[T]] [source]¶
Perform an interpolation over multiple dimensions.
- Parameters:
a – The “left” values.
b – The “right” values.
x – An array of how close the location of the final value should be to the “left” value.
- Returns:
A
numpy.ndarray
object.- Return type:
Usage:
>>> import numpy as np >>> >>> a = np.zeros((2, 3, 3), dtype=int) >>> b = np.full((2, 3, 3), 255, dtype=int) >>> x = np.linspace(0.0, 1.0, 18, True, False, float) >>> x = x.reshape((2, 3, 3)) >>> n_dimensional_interpolation(a, b, x, lerp) array([[135, 150, 165], [179, 194, 210], [225, 240, 255]])
- pjimg.util.n_dimensional_cubic_interpolation(a: ndarray[tuple[int, ...], dtype[T]], b: ndarray[tuple[int, ...], dtype[T]], x: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[T]] [source]¶
Perform a cubic interpolation over multiple dimensions. This is a shortcut for
lerpy.n_dimensional_interpolation()
usingcubic_interpolation()
as an interpolator.- Parameters:
a – The “left” values. The datatype of a also determines the datatype of the returned array.
b – The “right” values.
x – An array of how close the location of the final value should be to the “left” value.
- Returns:
A
numpy.ndarray
object.- Return type:
- pjimg.util.n_dimensional_linear_interpolation(a: ndarray[tuple[int, ...], dtype[T]], b: ndarray[tuple[int, ...], dtype[T]], x: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[T]] [source]¶
Perform a linear interpolation over multiple dimensions. This is a shortcut for
lerpy.n_dimensional_interpolation()
usinglinear_interpolation()
as an interpolator.- Parameters:
a – The “left” values. The datatype of a also determines the datatype of the returned array.
b – The “right” values.
x – An array of how close the location of the final value should be to the “left” value.
- Returns:
A
numpy.ndarray
object.- Return type:
resize¶
Functions for resizing numpy arrays.
- pjimg.util.build_resizing_matrices(src_shape: Sequence[int], dst_shape: Sequence[int]) tuple[ndarray[tuple[int, ...], dtype[uint8]], ndarray[tuple[int, ...], dtype[uint8]], ndarray[tuple[int, ...], dtype[float64]]] [source]¶
Create the indexing and distance arrays needed to interpolate values when resizing an array.
- pjimg.util.crop_array(a: ndarray[tuple[int, ...], dtype[T]], new_size: Sequence[int], loc: Sequence[int] = (0, 0, 0)) ndarray[tuple[int, ...], dtype[T]] [source]¶
Crop an array to a smaller size.
- Parameters:
a – The array to crop.
new_size – The size of the cropped array.
loc – (Optional.) How far to offset the crop from the center of the image. Defaults to no offset.
- Returns:
The cropped
numpy.ndarray
.- Return type:
- pjimg.util.magnify_size(shape: Sequence[int], factor: int) Sequence[int] [source]¶
Magnify the shape of an array.
- Parameters:
shape – The original shape of the array.
factor – The magnification factor.
- Returns:
A
tuple
containing the shape of the magnified array.
- pjimg.util.pad_array(a: ndarray[tuple[int, ...], dtype[T]], size: Sequence[int], fill: float = 0.0) ndarray[tuple[int, ...], dtype[T]] [source]¶
Pad an array to a larger size.
- Parameters:
a – The array to pad.
size – The shape of the size.
fill – The color of the padded area.
- Returns:
The padded
numpy.ndarray
.- Return type:
- pjimg.util.resize_array(src: ~numpy.ndarray[tuple[int, ...], ~numpy.dtype[~pjimg.util.model.T]], shape: ~typing.Sequence[int], interpolator: ~typing.Callable[[~numpy.ndarray[tuple[int, ...], ~numpy.dtype[~pjimg.util.model.T]], ~numpy.ndarray[tuple[int, ...], ~numpy.dtype[~pjimg.util.model.T]], ~numpy.ndarray[tuple[int, ...], ~numpy.dtype[~numpy.float64]]], ~numpy.ndarray[tuple[int, ...], ~numpy.dtype[~pjimg.util.model.T]]] = <function n_dimensional_linear_interpolation>) ndarray[tuple[int, ...], dtype[T]] [source]¶
Resize a two dimensional array using an interpolation.
- Parameters:
src – The array to resize. The array is expected to have at least two dimensions.
shape – The shape for the resized array.
interpolator – The interpolation algorithm for the resizing.
- Returns:
A
numpy.ndarray
object.- Return type:
util¶
General utility functions for pjimg
.
- pjimg.util.find_center(size: Sequence[int], loc: Sequence[int] = (0, 0, 0)) Sequence[int] [source]¶
Find the center pixel of an image of the given size.
- pjimg.util.get_free_rotation_size_2d(size: Sequence[int], pivot_offset: Sequence[int] = (0, 0, 0)) Sequence[int] [source]¶
Given the size of a final image, return the size of the image data you need to create in order to rotate the final image within the image data freely around the Z axis without the corners of the final image going outside of the bounds of the image data.
Basically, it says how big the image data needs to be so the corners of the final image won’t be black if you rotate the image.
debug¶
Utilities useful for debugging code that uses pjimg
.