コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
6861 ビュー

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)  

アバター
破棄
最善の回答

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)

アバター
破棄
著作者

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)

著作者

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

著作者

@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()))

著作者

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

著作者

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

著作者

link = image url*

著作者

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

著作者

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

著作者

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

著作者

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

関連投稿 返信 ビュー 活動
1
12月 22
2595
2
11月 22
2193
1
3月 22
3191
2
3月 22
3173
1
4月 25
1829