Maicoly Morocho
Maicoly Morocho

Reputation: 159

How to convert image (byte) to Base64 (str) Odoo?

i can't find a way to convert the image_1920 field to a string (base64), someone knows how to do it I would appreciate your help

I've done this but it doesn't convert it to str

base64.b64encode(record.image_1920)

The field is of this type!

enter image description here

Note: Odoo14 Community

ir_attachment enter image description here

How do I get the base64?

Upvotes: 0

Views: 1300

Answers (2)

Maicoly Morocho
Maicoly Morocho

Reputation: 159

what I did is bring the record ir.attachment, then I get the absolute path of the image file and that's it!

att = self.env['ir.attachment'].sudo().search([('id', '=', res[0])])
valores = self.env['ir.attachment'].sudo()._get_datas_related_values(att.datas,
                                                                         att.mimetype)
dir = self.env['ir.attachment']._full_path(valores['store_fname'])
with open(dir, "rb") as image_file:
        data = image_file.read()
        decode = data.decode("utf-8")
        return decode #base64 file

Upvotes: 1

adekock11
adekock11

Reputation: 614

The record.image_1920 is already Base64 encoded and you have to decode it. So try base64.b64decode(record.image_1920) instead of base64.b64encode(record.image_1920).

Upvotes: 0

Related Questions