-2

I want to create a class that deals with OpenCV images.

For that, I was thinking of passing to class already an image.

img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

class myClass():
   def __init__(self, frame):
      self.frame = frame

inst = myClass(img) 

But I want to pass my class only the path to the image. How can I do that my class can be able to take all the arguments taht cv2.imread takes?

Something like

inst = myClass(path, cv2.IMREAD_GRAYSCALE)
Sabzaliev Shukur
  • 361
  • 5
  • 12
  • Sure...just take the arguments and then make `img` in the `__init__`. Are you getting an error? – Matt Jan 10 '22 at 05:17

1 Answers1

1

Are you looking for something like:

class MyClass:
    def __init__(self, path, flags):
        self.frame = cv2.imread(path, flags)

inst = MyClass(path, cv2.IMREAD_GRAYSCALE)

Answering question asked below:

Note that that documentation for cv2.imread() says that it takes exactly two arguments, so that's precisely what I provided for. For a more general case:

class MyClass:
    def __init__(self, *args, **kwargs):
        self.frame = some_builder(*args, **kwargs)

would cause some_builder to be called with precisely the arguments (non-keyword and keyword) that the __init__ method was called with.

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22