python TypeError: a bytes-like object is required, not 'str'

使用python3.5.2和pytesseract的时候出现错误,代码和错误分别如下:

代码是这样的:

code:File "D:/test.py"

    # -*- coding: utf-8 -*-
    
    try:
        import Image
    except ImportError:
        from PIL import Image
    
    import pytesseract
    
    
    print(pytesseract.image_to_string(Image.open('d:/testimages/name.gif'), lang='chi_sim'))
    print(pytesseract.image_to_string(Image.open('d:/testimages/mobile.gif')))

error:

    Traceback (most recent call last):
      File "D:/test.py", line 11, in <module>
        print(pytesseract.image_to_string(Image.open('d:/testimages/name.gif'), lang='chi_sim'))
      File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 164, in image_to_string
        errors = get_errors(error_string)
      File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 112, in get_errors
        error_lines = tuple(line for line in lines if line.find('Error') >= 0)
      File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 112, in <genexpr>
        error_lines = tuple(line for line in lines if line.find('Error') >= 0)
    TypeError: a bytes-like object is required, not 'str'

怎么一回事?

阅读 5.6k
1 个回答

确定Image.open用对了吗?

 Image.open(fp, mode='r')
Source:
def open(fp, mode="r"):
    """
    Opens and identifies the given image file.

    This is a lazy operation; this function identifies the file, but
    the file remains open and the actual image data is not read from
    the file until you try to process the data (or call the
    :py:meth:`~PIL.Image.Image.load` method).  See
    :py:func:`~PIL.Image.new`.

    :param fp: A filename (string), pathlib.Path object or a file object.
       The file object must implement :py:meth:`~file.read`,
       :py:meth:`~file.seek`, and :py:meth:`~file.tell` methods,
       and be opened in binary mode.
    :param mode: The mode.  If given, this argument must be "r".
    :returns: An :py:class:`~PIL.Image.Image` object.
    :exception IOError: If the file cannot be found, or the image cannot be
       opened and identified.
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进