Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
6754 Visualizzazioni

Hello everyone, i am trying to get some image from odoo Image field but i always get the image size value like for example ( 47.99 Kb ). i want to get the image data, i dont care if its binary or anything (i can convert it later so yeah). Any help would be appreciated. Thanks!


For example this is the image field

image_image = fields.Image(string='Image',readonly='True',help='Generated Image' )

or this

image_variant_1920 = fields.Image("Variant Image", max_width=1920, max_height=1920)  

Avatar
Abbandona
Risposta migliore

To get the binary data for an image field, you need to decode it using base64.b64decode(Pass the image field) 

import base64
if self.image_1920:
    base64.b64decode(self.image_1920)

Avatar
Abbandona
Autore

Thanks for the reply, but i got this error. do note that template is one of the record of product.template model

line 54, in _compute_image_tes
image_data = base64.b64decode(template.image_1920)
File "/usr/lib/python3.8/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)

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

Traceback (most recent call last):
File "/home/smam/odoo15/odoo/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/smam/odoo15/odoo/http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
binascii.Error: Incorrect padding

Please share your code

try to check if image_1920 has value:
if template.image_1920:
image_data = base64.b64decode(template.image_1920)

Autore

image_1920 do have a value, but the value only come out as image size (one of the product, different product have different size). as i stated above for example is 47.99 Kb or maybe b'47.99 Kb' if i didn't decode it first

Autore

@api.depends('image_1920','company_id.key_imgbb')
def _compute_image_tes(self):
for template in self:
image_data = base64.b64decode(template.image_1920)
template.image_tes = str(image_data)

note : image_tes is string field

I tested it with write method and it's giving a value

def write(self,vals):
super().write(vals)
print(base64.b64decode(self.image_1920))

It seems it's not working with compute method, another solution to use image url:

@api.depends('image_1920', 'company_id.key_imgbb')
def _compute_image_tes(self):
for template in self:
url = template.get_base_url() + "/web/image/product.template/%s/image_1920" % template.id
template.image_tes = str(base64.b64encode(urlopen(url).read()))

Autore

thanks for your answer, i will try it. for the urlopen, what is the library or what do i need to import so i can use the urlopen?

from urllib.request import urlopen

Autore

Tried with link, it works. thank you so much!

Autore

link = image url*

Autore

One more thing, i got some error because the img sent is some kind of template image and not product image. do you know how to fix it? here is the template image that got sent https://i.ibb.co/t2SfQns/79912e68b28b.png

Make sure the product has an image

Autore

It has an image, i think its because the urlopen library isnt registered as user so odoo gave template image. because when i try to open the image url using browser with logged in account, i can see the image. but when i try to open with incognito (so logged out account) then i got that template image

Autore

Strangely i dont face this problem when i last sent my message, but now i got the problem

If ecommerce is installed you can get the image

Autore

can i get without ecommerce? not using urlopen but using odoo function or something?

You need to open access to model product.product and product.template. you can add security as below:

access_product_product_public,product.product.public,product.model_product_product,,1,0,0,0
access_product_template_public,product.template.public,product.model_product_template,,1,0,0,0

Post correlati Risposte Visualizzazioni Attività
1
dic 22
2555
2
nov 22
2153
1
mar 22
3154
2
mar 22
3140
1
apr 25
1795