콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
6991 화면

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
2697
2
11월 22
2277
1
3월 22
3323
2
3월 22
3269
1
7월 25
1938