cjm2671
cjm2671

Reputation: 19476

Extending numpy arrays

I'm new to Python/numpy.

I'm trying to extend numpy.array to give it some functions that make it nice for representing images (e.g. convert to greyscale etc).

import numpy as np
import cv2
from support import *
import matplotlib.pyplot as plt

class Frame(np.array):
    def __init__(self):
        print "new frame"


f = Frame()

currently this gives me:

  File "o.py", line 6, in <module>
    class Frame(np.array):
TypeError: Error when calling the metaclass bases
    cannot create 'builtin_function_or_method' instances

I don't understand why this is an issue for Python?

Upvotes: 3

Views: 933

Answers (1)

JoshAdel
JoshAdel

Reputation: 68682

You want to be subclassing np.ndarray, not np.array, but it's a little more complicated than just swapping on out for the other in your example. It's probably worth taking a look at the documentation: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

Upvotes: 2

Related Questions