You can use np.stack
to accomplish this much more concisely:
img = np.array([[1, 2], [3, 4]])
stacked_img = np.stack((img,)*3, axis=-1)
print(stacked_img)
# array([[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]]])
More Related Contents:
- Binning a column with Python Pandas
- Fitting empirical distribution to theoretical ones with Scipy (Python)?
- ValueError: setting an array element with a sequence
- Numpy `logical_or` for more than two arguments
- Simple Digit Recognition OCR in OpenCV-Python
- Best way to join / merge by range in pandas
- ImportError: numpy.core.multiarray failed to import
- How do you use the ellipsis slicing syntax in Python?
- Fastest way to grow a numpy numeric array
- Concatenate two NumPy arrays vertically
- How to make scipy.interpolate give an extrapolated result beyond the input range?
- Inverse Distance Weighted (IDW) Interpolation with Python
- Convert NumPy array to Python list
- Saving to hdf5 is very slow (Python freezing)
- Relationship between SciPy and NumPy
- Multivariate spline interpolation in python/scipy?
- Removing duplicate columns and rows from a NumPy 2D array
- How to conditionally combine two numpy arrays of the same shape
- scipy curve_fit doesn’t like math module
- Numpy Pure Functions for performance, caching
- Pythonic way of detecting outliers in one dimensional observation data
- Conditional assignment of tensor values in TensorFlow
- How to fix ‘numpy.ndarray’ object has no attribute ‘get_figure’ when plotting subplots
- Make distutils look for numpy header files in the correct place
- LogisticRegression: Unknown label type: ‘continuous’ using sklearn in python
- How to determine whether a column/variable is numeric or not in Pandas/NumPy?
- Python OpenCV2 (cv2) wrapper to get image size?
- Stratified Sampling in Pandas
- Numpy transpose of 1D array not giving expected result
- Speedup scipy griddata for multiple interpolations between two irregular grids