Skip to content Skip to sidebar Skip to footer

Iterating Over A Numpy Array And Operating On Each Element

I have a numpy array of size 8x8. Here is the numpy array: QuantTable = np.array([[16, 11 ,10, 16, 24, 40, 51, 61], [12, 12, 14, 19, 26, 58, 60, 55],

Solution 1:

Just remove the loops, and the indexing. Numpy automatically broadcasts those operations. Also, a lot of your code can be taken out of the if...else statements.

def quantizationTable(Qval, QuantTable):
    QuantTable = np.asarray(QuantTable, dtype=np.float32)
    if int(Qval) < 50:
        scalingFactor = 5000 / Qval
    else:
        scalingFactor = 200 - 2 * Qval # confirm that this is what you want? 

    QuantTable *= scalingFactor + 0.5
    QuantTable[QuantTable == 0] = 1

    return QuantTable

Post a Comment for "Iterating Over A Numpy Array And Operating On Each Element"