Reshape a Buffer into an Image
~8 mincode completion
Implement image_from_buffer(pixels, height, width, channels) returning an array of shape (height, width, channels).
Examples
1x2 RGB: six values become one row of two pixels
- Input
- image_from_buffer([1, 2, 3, 4, 5, 6], 1, 2, 3)
- Output
- [[[1, 2, 3], [4, 5, 6]]]
2x2 RGB: twelve values, two rows
- Input
- image_from_buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 2, 2, 3)
- Output
- [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
Hints
Hint 1
Reshape so the two arrays broadcast against each other.
Hint 2
Reach for row major rather than fortran order.
Requirements
pixels: 1D array of length height width channelsheight: number of rowswidth: number of columnschannels: number of colour channelsReturn array of shape (height, width, channels)
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~8 min
••••••••••••••••
8 employers weight this skill
4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 93.
Python
import numpy as np
def image_from_buffer(pixels, height, width, channels):
"""
Reshape a flat pixel buffer into channel-last (H, W, C) layout.
Args:
pixels: 1D array of length height * width * channels
height: number of rows
width: number of columns
channels: number of colour channels
Returns:
array of shape (height, width, channels)
"""
# YOUR CODE HERE
pass