python-3.xnumpypython-camelot

how can I construct a list of NumPy arrays from these two arrays?


I have two arrays which are column and row values in PDF coordinate space:

x = array([111, 303, 405, 513]
y = array([523, 546 , 569 , 603 ])

Here's a visual:

enter image description here

I need to convert this into a list of numpy arrays, where each array is the boundary coordinates (four points that define the box). For example, a list of the lower left and upper right boxes would be

all_boxes =
    [array([[111, 523],
            [303, 523],
            [303, 546],
            [111, 546]], dtype=int64),
    array([[405, 569],
            [513, 569],
            [513, 603],
            [405, 603]], dtype=int64)]

And so on for nine boxes. How can I achieve this? I would guess some kind of NumPy multiplication but I can't see it. Thank you.


Solution

  • Short and simple:

    import numpy as np
    x = np.array([111, 303, 405, 513])
    y = np.array([523, 546 , 569 , 603 ])
    
    def coupler(lst): return [[c1,c2] for c1,c2 in zip(lst,lst[1:])]
    
    x_couples,y_couples=coupler(x),coupler(y)
    
    boxes=[]
    for x1,x2 in x_couples:
        for y1,y2 in y_couples:
            boxes.append(np.array([[x1,y1],[x2,y1],[x2,y2],[x1,y2]]))
    

    The output:

    [array([[111, 523],
            [303, 523],
            [303, 546],
            [111, 546]]), 
    array([[111, 546],
            [303, 546],
            [303, 569],
            [111, 569]]),...]