0

I've an image processing task and we're prohibited to use NumPy so we need to code from scratch. I've done the logic image transformation but now I'm stuck on creating an array without numpy.

So here's my last output code :

Output :
new_log =
[[236, 
  232, 
  226, 
  .
  .
  .
 198,
 204]]

I need to convert this to an array so I can write the image like this (with Numpy)

new_log =
array([[236, 232, 226, ..., 208, 209, 212],
       [202, 197, 187, ..., 198, 200, 203],
       [192, 188, 180, ..., 205, 206, 207],
       ...,
       [233, 226, 227, ..., 172, 189, 199],
       [235, 233, 228, ..., 175, 182, 192],
       [235, 232, 228, ..., 195, 198, 204]], dtype=uint8)
cv.imwrite('log_transformed.jpg', new_log) 
# new_log must be shaped like the second output
panji gemilang
  • 759
  • 2
  • 10
  • 25

1 Answers1

0

You can make a straightforward function to take your list and reshape it in a similar way to NumPy's np.reshape(). But it's not going to be fast, and it doesn't know anything about data types (NumPy's dtype) so... my advice is to challenge whoever it is that doesn't like NumPy. Especially if you're using OpenCV — it depends on NumPy!

Here's an example of what you could do in pure Python:

def reshape(l, shape):
    """Reshape a list.

    Example
    -------
    >>> l = [1,2,3,4,5,6,7,8,9]
    >>> reshape(l, shape=(3, -1))
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    """
    nrows, ncols = shape
    if ncols == -1:
        ncols = len(l) // nrows
    if nrows == -1:
        nrows = len(l) // ncols
    array = []
    for r in range(nrows):
        row = []
        for c in range(ncols):
            row.append(l[ncols*r + c])
        array.append(row)
    return array
Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • what does -1 mean? – panji gemilang Sep 25 '19 at 13:17
  • In NumPy's `reshape()` function, it's a sort of wildcard for "I don't know". It's how you say, for example, "I know I want 3 rows, you figure out how many columns I need, given the number of elements in my array". – Matt Hall Sep 25 '19 at 14:02