【问题标题】:Why is pytesseract causing AttributeError: 'NoneType' object has no attribute 'bands'?为什么 pytesseract 导致 AttributeError:'NoneType' 对象没有属性 'bands'?
【发布时间】:2015-09-25 22:30:25
【问题描述】:

我正在尝试开始使用 pytesseract,但正如您在下面看到的那样,我遇到了问题。

我发现人们遇到了似乎相同的错误,他们说这是 PIL 1.1.7 中的错误。其他人说问题是由 PIL 懒惰引起的,需要强制 PIL 在打开图像后使用im.load() 加载图像,但这似乎没有帮助。任何建议都非常感谢。

K:\Glamdring\Projects\Images\OCR>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> import pytesseract
>>> pytesseract.image_to_string(Image.open('foo.png'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win32\egg\pytesseract\pytesseract.py", line 143, in image_to_string
  File "c:\Python27_32\lib\site-packages\PIL\Image.py", line 1497, in split
    if self.im.bands == 1:
AttributeError: 'NoneType' object has no attribute 'bands'

【问题讨论】:

  • 你能发布导致这个问题的图片吗?

标签: python-2.7 python-imaging-library ocr


【解决方案1】:

尝试分别使用 Image 和 pytesseract 模块中的对象。
它解决了我的问题:

try:
    import Image
except ImportError:
    from PIL import Image
import pytesseract

img = Image.open('myImage.jpg')
img.load()
i = pytesseract.image_to_string(img)
print i

【讨论】:

    【解决方案2】:

    我之前没有使用 PIL 的经验,但我很无聊,所以我试图调查它,据我所知,它是 可能是一个错误。


    如果我们查看执行步骤,这不是 pytesseract 的错。

    • 最初,您的 Image.open('foo.png') 工作正常,没有与堆栈跟踪相关的错误。
    • pytesseract.image_to_string(img) 随后进入并执行以下操作:

      # Omitting the rest of the method.
      
      # calls method split() of your image object.
      if len(image.split()) == 4:
      

      这是作用于image第一个 语句,因此我们知道我们必须回头查看PIL 才能找到问题的根源。

    • 您的堆栈跟踪具有与 if self.im.bands 语句有关的特定消息 AttributeError: 'NoneType' object has no attribute 'bands'。这意味着imobject = None

      让我们看看image.split() 方法:

      """
      Split this image into individual bands. This method returns a
      tuple of individual image bands from an image. For example,
      splitting an "RGB" image creates three new images each
      containing a copy of one of the original bands (red, green,
      blue).
      
      :returns: A tuple containing bands.
      """
      
      self.load() # This is the culprit since..
      if self.im.bands == 1: # .. here the im attribute of the image = None
          ims = [self.copy()]
      
      # Omitting the rest ---
      

      显然self.load() 设置了im 值等。我用测试图像验证了这一点,它似乎没有任何问题[我建议你对你的图像尝试同样的操作]:

      In [7]: print img.im
      None
      
      In [8]: img.load()
      Out[8]: <PixelAccess at 0x7fe03ab6a210>
      
      In [9]: print img.im
      <ImagingCore object at 0x7fe03ab6a1d0>
      
    • 现在让我们看看load(): 我一般不知道这里的内部情况,但我确实观察到了一些不确定的事情:在分配im 之前有许多 FIXME cmets,具体来说:

      # -- Omitting rest --         
      
      # FIXME: on Unix, use PROT_READ etc
      self.map = mmap.mmap(file.fileno(), size)
      self.im = Image.core.map_buffer(
                          self.map, self.size, d, e, o, a
                          )
      
      # -- Omitting rest --
      
      if hasattr(self, "tile_post_rotate"):
          # FIXME: This is a hack to handle rotated PCD's
          self.im = self.im.rotate(self.tile_post_rotate)
          self.size = self.im.size
      

      这可能表明这里可能存在一些需要注意的问题。 但我不能 100% 确定。


    当然,这可能是由 您的图片 造成的。 load() 方法适用于我提供的图像(而pytesseract 只是给了我一个不同的错误:P)。您最好为此创建一个新的issue。如果有任何PIL 专家碰巧看到这一点,如果可以的话,请赐教。

    【讨论】:

    • 感谢您的关注和建议。我对 PIL 的了解不够,无法调试(如果是错误)模块,所以我会尝试寻找其他方法来做我想做的事。
    • 只是出于好奇,Image.open('foo.png').load() 是否返回 None
    【解决方案3】:

    im.load() 在管理员模式下运行程序时为我工作,如果您的 PATH 中没有 tesseract 可执行文件,请添加此行

    pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
    

    如果您已经读取了图像(不是使用 im.load() 而是使用 imread())或视频帧,并且对该变量(图像)进行了一些图像处理(可能不是),那么您需要给出以下命令 pytesseract.image_to_string(Image.fromarray(image))

    【讨论】:

      【解决方案4】:

      正如@J_Mascis 所说,使用对象也可以在这里工作-

          import pytesseract
          from PIL import Image
          img = Image.open('im.jpg')
          img.load()
      
          print(pytesseract.image_to_string(img, lang='eng'))#'eng' for english
      

      【讨论】:

      • 那你为什么发布重复的答案?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      相关资源
      最近更新 更多