OCA / odoorpc

Python module to pilot your Odoo servers through JSON-RPC.
http://pythonhosted.org/OdooRPC/
GNU Lesser General Public License v3.0
231 stars 123 forks source link

fetching binary data of image #96

Open abaysevi opened 6 months ago

abaysevi commented 6 months ago

i was trying to fetch the binary data of an image from the products image_1920

also i am using flask

`@app.route('/products') def list_products(): try:

Fetch product data

    res = odoo.execute_kw("product.template", "search_read", [], {
        'context': {'bin_size': True},
        'domain': [],
        'fields': ['id', 'name', 'list_price', 'default_code', 'image_1920'],
        'limit': 80,
    })

    # Extract and decode binary image data (if present)
    products=[]
    for product in res:
        if product.get('image_1920'):
            try:
                print(type(product.get("image_1920")))
                image_data = base64.b64decode(product['image_1920'])
            except Exception:
                try:
                    image_data = base64.urlsafe_b64decode(product['image_1920'])
                except Exception:
                    image_data = None  # Handle unknown encoding

            product['image_data'] = image_data
            products.append(product)
    return jsonify({"message": "data received", "data": products})

except Exception as e:
    return jsonify({'fetching data': False, 'message': str(e)})`

    in this the response i get for the image_1920 

    is like a string with the size of the image 

33.53 Kb 25.06 Kb 30.60 Kb 49.69 Kb

like this

{'id': 41, 'name': 'Whiteboard Pen', 'list_price': 1.2, 'default_code': 'CONS_0001', 'image_1920': '44.87 Kb'}

i need the binary data