Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How should I replace the numpy array in this code?

I have a program that creates a linear gradient but uses numpy arrays. This cannot be used because the numpy library takes up too much space on the microcontroller. Is there a way to replace the numpy.array with something else?

import numpy as np

def colorFade(c1, c2, mix=0):
    c1 = np.array(c1)
    c2 = np.array(c2)
    print(tuple((1-mix)*c1 + mix*c2))

c1 = (0, 0, 200)
c2 = (200, 0, 200)
n = 500

for x in range(n+1):
    colorFade(c1,c2,x/n)

I attempted to change the array to a tuple and a list but both failed and gave this error:
TypeError: can't multiply sequence by non-int of type 'float'
The numpy.array module does not give this error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Lists and tuples don’t automatically apply arithmetic operations to each element, you have to write that explicitly.

def colorFade(c1, c2, mix=0):
    print(tuple((1-mix)*e1 + mix*e2 for e1, e2 in zip(c1, c2)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading