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

Import image with Odoo API

Subscribe

Get notified when there's activity on this post

This question has been flagged
imageapiodoo
1 Reply
10972 Views
Avatar
Yves Bourgois

Hi Guys,

I'm working on a script to import a list of product in my Odoo database. The script is working really fine with the Odoo API, except for importing the image.
What I have done so far, is to import an image from an URL in python, and then, encoding it in base64: 

        response = requests.get(image_url)
img_obj = Image.open(BytesIO(response.content))
buffer = BytesIO()
img_obj.save(buffer, format="JPEG")
img = base64.encodestring(buffer.getvalue()).strip()
try:
base64.b64decode(img)
except TypeError:
padding = len(img) % 4
if padding == 1:
img = "Invalid base64 string: {}".format(img)
elif padding == 2:
img += b'=='
elif padding == 3:
img += b'='

This code is working great. But, when I run the python code to import it with Odoo API, here is what I have:
mlrpc.client.Fault: <Fault 1: 'Traceback (most recent call last):\n File "/opt/odoo/openerp/service/wsgi_server.py", line 56, in xmlrpc_return\n result = openerp.http.dispatch_rpc(service, method, params)\n File "/opt/odoo/openerp/http.py", line 114, in dispatch_rpc\n result = dispatch(method, params)\n File "/opt/odoo/openerp/service/model.py", line 37, in dispatch\n res = fn(db, uid, *params)\n File "/opt/odoo/openerp/service/model.py", line 173, in execute_kw\n return execute(db, uid, obj, method, *args, **kw or {})\n File "/opt/odoo/openerp/service/model.py", line 118, in wrapper\n return f(dbname, *args, **kwargs)\n File "/opt/odoo/openerp/service/model.py", line 181, in execute\n res = execute_cr(cr, uid, obj, method, *args, **kw)\n File "/opt/odoo/openerp/service/model.py", line 170, in execute_cr\n return getattr(object, method)(cr, uid, *args, **kw)\n File "/opt/odoo/openerp/api.py", line 238, in wrapper\n return old_api(self, *args, **kwargs)\n File "/opt/odoo/addons/stock/product.py", line 518, in write\n return super(product_template, self).write(cr, uid, ids, vals, context=context)\n File "/opt/odoo/openerp/api.py", line 238, in wrapper\n return old_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 369, in old_api\n result = method(recs, *args, **kwargs)\n File "/opt/odoo/addons/account/models/product.py", line 45, in write\n res = super(ProductTemplate, self).write(vals)\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 562, in new_api\n result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)\n File "/opt/odoo/addons/product/product.py", line 705, in write\n res = super(product_template, self).write(cr, uid, ids, vals, context=context)\n File "/opt/odoo/openerp/api.py", line 238, in wrapper\n return old_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 369, in old_api\n result = method(recs, *args, **kwargs)\n File "/opt/odoo/addons/mail/models/mail_thread.py", line 274, in write\n result = super(MailThread, self).write(values)\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/models.py", line 3820, in write\n self._write(old_vals)\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 562, in new_api\n result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)\n File "/opt/odoo/openerp/models.py", line 3971, in _write\n result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []\n File "/opt/odoo/openerp/osv/fields.py", line 646, in set\n \'datas\': value,\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 478, in new_api\n result = method(self._model, cr, uid, *args, **old_kwargs)\n File "/opt/odoo/openerp/addons/base/ir/ir_attachment.py", line 459, in create\n values[\'mimetype\'] = self._compute_mimetype(values)\n File "/opt/odoo/openerp/addons/base/ir/ir_attachment.py", line 252, in _compute_mimetype\n mimetype = guess_mimetype(values[\'datas\'].decode(\'base64\'))\n File "/usr/lib/python2.7/xmlrpclib.py", line 503, in decode\n self.data = base64.decodestring(data)\n File "/usr/lib/python2.7/base64.py", line 321, in decodestring\n return binascii.a2b_base64(s)\nError: Incorrect padding\n'>

I have tried to correct the base64 padding, (as you can see in my previous code), but that's not working. Did anyone know how can I fixe this problem?

Thanks,

Don

0
Avatar
Discard
Cybrosys Techno Solutions Pvt.Ltd

Try refer the following forum post
https://www.odoo.com/sl_SI/forum/help-1/how-to-import-image-from-api-159947

Avatar
shreeram
Best Answer

   path = 'logo1.png'

   # get file

   with open(path, 'rb') as f:

        contents = f.read()

   imageBase64 = base64.b64encode(contents) #convert to base64

   imageBase64.decode() #pass the value of this decode result


Mail me for any question: shreeramyadav1993@gmail.com

1
Avatar
Discard
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
Odoo Session Expired Error When Accessing API Endpoint
api odoo
Avatar
Avatar
2
Dec 24
3235
How to set image upload size?
image odoo
Avatar
Avatar
1
Jan 23
3323
How to get data with rest API and post it in odoo view? Solved
api odoo
Avatar
Avatar
1
Feb 22
4721
Create a module , with CRUD operation using external API
api odoo
Avatar
Avatar
1
Feb 22
7844
Possible to create contact through API call?
api odoo
Avatar
0
Nov 21
5289
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