# [Numpy-discussion] Overlap arrays with "transparency"

Cristi Constantin darkgl0w@yahoo....
Wed May 20 06:24:43 CDT 2009

```Thank you for your help. :)

I used this :
try: NData[ (NData==transparent)[:len(OData)] ] = OData[ (NData==transparent)[:len(OData)] ]
except: pass

That means overwrite all "transparent" data from NData with valid data from OData.
I am sure it's not the best method yet, but it's the only one that works.

--- On Mon, 5/18/09, Cristi Constantin <darkgl0w@yahoo.com> wrote:

From: Cristi Constantin <darkgl0w@yahoo.com>
Subject: [Numpy-discussion] Overlap arrays with "transparency"
To: "Numpy Discussion" <numpy-discussion@scipy.org>
Date: Monday, May 18, 2009, 5:37 AM

Good day.
I am working on this algorithm for a few weeks now, so i tried almost everything...
I want to overlap / overwrite 2 matrices, but completely ignore some values (in this case ignore 0)
Let me explain:

a = [
[1, 2, 3, 4, 5],
[9,7],
[0,0,0,0,0],
[5,5,5] ]

b = [
[0,0,9,9],
[1,1,1,1],
[2,2,2,2] ]

Then, we have:

a over b = [
[1,2,3,4,5],
[9,7,1,1],
[1,1,1,1,0],
[5,5,5,2] ]

b over a = [
[0,0,9,9,5],
1,1,1,1],
2,2,2,2,0],
5,5,5] ]

That means, completely overwrite one list of arrays over the other, not matter what values one has, not matter the size, just ignore 0 values on overwriting.
I checked the documentation, i just need some tips.

TempA = [[]]
#
One For Cicle in here to get the Element data...
Data =
vElem.data                 # This is a list of numpy ndarrays.
#
for nr_row in range( len(Data) ): # For each numpy ndarray (row) in Data.
#
NData = Data[nr_row]                   # New data, to be written over old data.
OData = TempA[nr_row:nr_row+1] or [[]] # This is old data. Can be numpy ndarray, or empty list.
OData = OData[0]
#
# NData must completely eliminate transparent pixels... here comes the algorithm... No algorithm
yet.
#
if len(NData) >= len(OData):
# If new data is longer than old data, old data will be completely overwritten.
TempA[nr_row:nr_row+1] = [NData]
else: # Old data is longer than new data ; old data cannot be null.
TempB = np.copy(OData)
TempB.put( range(len(NData)), NData )
#TempB[0:len(NData)-1] = NData # This returns "ValueError: shape mismatch: objects cannot be broadcast to a single shape"

TempA[nr_row:nr_row+1] = [TempB]
del TempB
#
#
#
The result is stored inside TempA as list of numpy arrays.

I would use 2D arrays, but they are slower than Python Lists containing Numpy arrays. I need to do this overwrite in a very big loop and every delay is very important.
I tried to create a masked array where all "zero" values are ignored on overlap, but it doesn't work. Masked or not, the "transparent" values are still overwritten.