[Odoo 11.0, Python 3]Save Images with base64 encoding

Ermin Trevisan

I'm still working on porting the module to Odoo 11.0.

When trying to save an PNG image with the following code in the file

def _get_default_image(self):
with open(modules.get_module_resource('product_default_image',
'rb') as f:
return base64.b64encode(

product_image = fields.Binary(
string='Global Product Image',
help='Use as the global image for all product default images. '
'Limited to 1024x1024.',

def write(self, vals):
""" Changes product images if target or product_image have changed """

target = vals.get('product_image_target')

if 'product_image' not in vals and not target:
return super(ResCompany, self).write(vals)

if 'product_image' in vals:
vals['product_image'] = \

I get the following error message:

Odoo Server Error

Traceback (most recent call last):
File "/usr/lib/python3.6/", line 510, in _input_type_check
m = memoryview(s)
TypeError: memoryview: a bytes-like object is required, not 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/", line 19, in base64_decode
return (base64.decodebytes(input), len(input))
File "/usr/lib/python3.6/", line 545, in decodebytes
File "/usr/lib/python3.6/", line 513, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/", line 653, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/", line 312, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/", line 87, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/", line 695, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/", line 344, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/", line 337, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/", line 939, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/", line 517, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/", line 934, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/", line 926, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/", line 687, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/", line 672, in call_kw_model
result = method(recs, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/account/models/", line 152, in create
return super(ResConfigSettings, self).create(values)
File "/usr/lib/python3/dist-packages/odoo/", line 3402, in create
File "/usr/lib/python3/dist-packages/odoo/", line 1082, in determine_inverse
File "/usr/lib/python3/dist-packages/odoo/", line 599, in _inverse_related
other[] = record_value[record]
File "/usr/lib/python3/dist-packages/odoo/", line 4794, in __setitem__
return self._fields[key].__set__(self, value)
File "/usr/lib/python3/dist-packages/odoo/", line 987, in __set__
record.write({ write_value})
File "/usr/lib/python3/dist-packages/odoo/addons/product_default_image/models/", line 76, in write
File "/usr/lib/python3/dist-packages/odoo/tools/", line 143, in image_resize_image_big
return image_resize_image(base64_source, size, encoding, filetype, avoid_if_small)
File "/usr/lib/python3/dist-packages/odoo/tools/", line 56, in image_resize_image
image_stream = io.BytesIO(codecs.decode(base64_source, encoding))
TypeError: decoding with 'base64' codec failed (TypeError: expected bytes-like object, not str)

How do I correctly code this for Python3/Odoo 11?

Thanks for any hints.

1 Answer
Best Answer
1 Comment
Ermin Trevisan

Thank you for your hint, I understand the direction, but why is it no handled by the tools from in Odoo 11? I have amended my code in the post. Is there something missing in my @api.model?