3

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?

2
  • 2
    Just as a side note you should check out: PIL Python Imaging Library pythonware.com/products/pil/#pil117. It has these abilities and converting to/from ndarray is quite simple Commented Apr 16, 2014 at 16:44
  • 1
    And if you are going to go with PIL, you should try its fork pillow, which is a more updated version that is also compatible with Python 3. Commented Apr 16, 2014 at 16:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. In the end, I classed 'around' it, rather than extend it, looks like the safer option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.