I have a large image A
and a smaller image B
, both expressed as 2-D numpy
arrays. I want to use A
as the canvas, and write translated copies of B
all over it, packed in a hexagonal arrangement. The part I can't get my head around is how to handle it such that the image wraps both vertically and horizontally—essentially what I want is regular tessellation of a (padded, as necessary) sub-image onto a torus.
I've seen the discussion of numpy.take
and numpy.roll
at
wrapping around slices in Python / numpy and that shows me how to access and return a copy of a wrapped slice of an array, but I want to assign to that—i.e., for arbitrary integers rowOffset
and columnOffset
I want to do the equivalent of:
A = numpy.zeros((5,11), int)
B = numpy.array([[1,2,3,4,5,6,7]]) * numpy.array([[10,100,1000]]).T
# OK, we wouldn't be able to fit more than one or two copies of B into A, but they demonstrate the wrapped placement problem
wrappedRowIndices = ( numpy.arange(B.shape[0]) + rowOffset ) % A.shape[0]
wrappedColumnIndices = ( numpy.arange(B.shape[1]) + columnOffset ) % A.shape[1]
A[ wrappedRowIndices, : ][ :, wrappedColumnIndices ] = B
I see from a comment on the question,
and from a moment's reflection on the way numpy
arrays are represented, that there's no way a wrapped slice can be returned as a view
in the way this demands.
Is there (Y) a way of assigning to wrapped slices of an array in this way, or (X) an existing utility for performing the kind of tessellation I'm trying to achieve?