TypeError: file() argument 1 must be encoded string without NULL bytes, not str odoo

TypeError: file() argument 1 must be encoded string without NULL bytes, not str odoo

python版本是2.8.2,在eclipse中使用。当我尝试执行以下命令时出现此错误:

函数是:

@api.one
@api.depends('image')
def _compute_image_details(self):
    if self.image:
        image_content = self.image.decode('base64')
        print type(self.image)
        print type(image_content)

        # File size
        self.size = len(image_content)

        # Camera make and model from EXIF tags
        img = PIL.Image.open(image_content)
        exif_tags = img._getexif()

        # 0x010f is a numeric code for the "make" exif field
        # You can find a list of fields here: exiv2.org/tags.html
        self.camera_maker = exif_tags.get(0x010f)

引发的错误是:

Traceback (most recent call last):
  File "/home/next/WORKSPACE/odoo-8.0/openerp/http.py", line 537, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/http.py", line 574, in dispatch
    result = self._call_function(**self.params)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/http.py", line 310, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/service/model.py", line 113, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/http.py", line 307, in checked_call
    return self.endpoint(*a, **kw)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/http.py", line 803, in __call__
    return self.method(*args, **kw)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/http.py", line 403, in response_wrap
    response = f(*args, **kw)
  File "/home/next/WORKSPACE/odoo-8.0/addons/web/controllers/main.py", line 944, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/addons/web/controllers/main.py", line 936, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/api.py", line 363, in old_api
    result = method(recs, *args, **kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/models.py", line 5873, in onchange
    newval = record[name]
  File "/home/next/WORKSPACE/odoo-8.0/openerp/models.py", line 5571, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "/home/next/WORKSPACE/odoo-8.0/openerp/fields.py", line 820, in __get__
    self.determine_draft_value(record)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/fields.py", line 928, in determine_draft_value
    self._compute_value(record)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/fields.py", line 867, in _compute_value
    self.compute(records)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/api.py", line 239, in wrapper
    return new_api(self, *args, **kwargs)
  File "/home/next/WORKSPACE/odoo-8.0/openerp/api.py", line 397, in new_api
    result = [method(rec, *args, **kwargs) for rec in self]
  File "/home/next/WORKSPACE/odoo-8.0/addons/transform_webservice_example/image_example.py", line 33, in _compute_image_details
    img = PIL.Image.open(image_content)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1955, in open
    fp = __builtin__.open(fp, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

请帮我解决这个问题。

其他参考资料说的是文件的URL。这是不同的场景。

您正在尝试打开文件的 base64 解码内容,您应该打开文件本身。

PIL.Image.open(self.image)

以此为例,重现您遇到的错误

>>> open('[=11=]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
>>>

字符'[=12=]'被视为解码图像包含的NullByte