-3

I try to run this code fragment. And I don't get any errors at compile time. But if I run the script. I get this errors:

File "c:\Users\engel\Documents\python\code\textFromImages.py", line 8, in <module>
    class Text_from_image:
  File "c:\Users\engel\Documents\python\code\textFromImages.py", line 47, in Text_from_image
    print(filterAnanas())
TypeError: Text_from_image.filterAnanas() missing 1 required positional argument: 'self'
class Text_from_image:
    
    #class variables:
    apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
    ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
    peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'    
    
    def __init__(self) :
        pass        

    def make_pattern(substr):
        return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)\n"

    def get_text_from_image():
        pdfFile = wi(
            filename="C:\\Users\\engel\\Documents\\python\\docs\\fixedPDF.pdf", resolution=300)
        text_factuur_verdi = []

        image = pdfFile.convert('jpeg')
        imageBlobs = []
        

        for img in image.sequence:
            imgPage = wi(image=img)
            imageBlobs.append(imgPage.make_blob('jpeg'))

        for imgBlob in imageBlobs:
            image = Image.open(io.BytesIO(imgBlob))
            text = pytesseract.image_to_string(image, lang='eng')
            text_factuur_verdi.append(text)

        return text_factuur_verdi

    
    def filterAnanas(self):
        self.get_text_from_image()
        return re.findall(self.make_pattern(self.ananas_crownless), self.text_factuur_verdi[0])

    if ananas_crownless:
        print(filterAnanas())

So my question is how to fix this?

wovano
  • 4,543
  • 5
  • 22
  • 49
mightycode Newton
  • 3,229
  • 3
  • 28
  • 54
  • 2
    `filterAnanas` is a method of the class `Text_from_image`. You can only call the method from an instance of the class, like this `Text_from_image().filterAnanas()` – Rajarishi Devarajan Sep 28 '22 at 08:54
  • Does this answer your question? [Type Error missing 1 required positional argument: 'self' calling the class function](https://stackoverflow.com/questions/71456917/type-error-missing-1-required-positional-argument-self-calling-the-class-func) – wovano Sep 28 '22 at 08:54
  • `filterAnanas` is an instance method, you need to have an instance of the `Text_from_image` class in order to call that method. You need to read the basics about how classes and methods in Python work – Anentropic Sep 28 '22 at 08:55
  • if `make_pattern` and `get_text_from_image` don't take a `self` arg they should be decorated with `@staticmethod` – Anentropic Sep 28 '22 at 08:55
  • @wovano I don't think it's a proper dup. The suggested post has it the other way around: `self` is mistakenly added to a static method. – bereal Sep 28 '22 at 08:56
  • Ah wait. I didn't read the code to the end. So you're trying to call the method even before the class definition is complete. That's not how it all is supposed to work. Instantiate the class and then call the method. Just what people above say. – bereal Sep 28 '22 at 09:03
  • @bereal I think the proposed duplicate contains enough information and examples of how to do this. But yeah, there probably is a better duplicate target. There are many questions about this... – wovano Sep 28 '22 at 09:12

1 Answers1

0

There are a lot of bugs in there, I have tried to fix most of them with this...

class Text_from_image:


    def __init__(self):
        # class variables:
        self.apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
        self.ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
        self.peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'

    @staticmethod
    def make_pattern(substr):
        return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)\n"

    @staticmethod
    def get_text_from_image():
        pdfFile = wi(
            filename="C:\\Users\\engel\\Documents\\python\\docs\\fixedPDF.pdf", resolution=300)
        text_factuur_verdi = []

        image = pdfFile.convert('jpeg')
        imageBlobs = []

        for img in image.sequence:
            imgPage = wi(image=img)
            imageBlobs.append(imgPage.make_blob('jpeg'))

        for imgBlob in imageBlobs:
            image = Image.open(io.BytesIO(imgBlob))
            text = pytesseract.image_to_string(image, lang='eng')
            text_factuur_verdi.append(text)

        return text_factuur_verdi

    def filterAnanas(self):
        return re.findall(self.make_pattern(self.ananas_crownless), self.get_text_from_image()[0])

    if self.ananas_crownless:
        print(filterAnanas())
mrw
  • 142
  • 5