Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

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

Subscribe

Get notified when there's activity on this post

This question has been flagged
base64python3odoo11.0
1 Reply
33609 Views
Avatar
Ermin Trevisan

I'm still working on porting the module https://www.odoo.com/apps/modules/10.0/product_default_image/ to Odoo 11.0.

When trying to save an PNG image with the following code in the file rescompany.py:

@api.model
def _get_default_image(self):
with open(modules.get_module_resource('product_default_image',
'static/src/img',
'glob_prod_img.png'),
'rb') as f:
return base64.b64encode(f.read())

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


@api.multi
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'] = \
tools.image_resize_image_big(
base64_source=vals['product_image'],
)

I get the following error message:

Error:
Odoo Server Error

Traceback (most recent call last):
File "/usr/lib/python3.6/base64.py", 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/base64_codec.py", line 19, in base64_decode
return (base64.decodebytes(input), len(input))
File "/usr/lib/python3.6/base64.py", line 545, in decodebytes
_input_type_check(s)
File "/usr/lib/python3.6/base64.py", 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/http.py", line 653, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 312, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 87, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/http.py", line 695, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 344, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 337, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 939, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 517, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 934, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 926, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 687, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 672, in call_kw_model
result = method(recs, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/account/models/res_config_settings.py", line 152, in create
return super(ResConfigSettings, self).create(values)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 3402, in create
self._fields[key].determine_inverse(record)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1082, in determine_inverse
self.inverse(records)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 599, in _inverse_related
other[field.name] = record_value[record]
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4794, in __setitem__
return self._fields[key].__set__(self, value)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 987, in __set__
record.write({self.name: write_value})
File "/usr/lib/python3/dist-packages/odoo/addons/product_default_image/models/res_company.py", line 76, in write
base64_source=vals['product_image'],
File "/usr/lib/python3/dist-packages/odoo/tools/image.py", 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/image.py", 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
Avatar
Discard
Avatar
Rajkumar
Best Answer

Hi,

Refer this link

https://stackoverflow.com/questions/2323128/convert-string-in-base64-to-image-and-save-on-filesystem-in-python

​


2
Avatar
Discard
Ermin Trevisan
Author

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

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
make my function work forever
python3 odoo11.0
Avatar
Avatar
1
Jan 20
3718
add python function to specific action
python3 odoo11.0
Avatar
0
Jan 20
4007
valid logged in user
python3 odoo11.0
Avatar
Avatar
1
Dec 19
4807
Add dynamic domain to allocation leave type
onchange python3 odoo11.0
Avatar
Avatar
1
May 24
3761
Hide fields in Filters And Group By Solved
python3 odoo11.0 Jquery
Avatar
Avatar
Avatar
3
Jun 23
17089
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now