【问题标题】:Store odoo images into filesystem将 odoo 图像存储到文件系统中
【发布时间】:2018-04-10 06:52:32
【问题描述】:

希望所有堆栈成员都没事。我能够使用代码获取产品图像的二进制数据

p_ids=self.env.context.get('active_ids')
produtc_templates = self.env['product.template']
for p_id in p_ids:
 binaryData = produtc_templates.search([('id', '=',p_id)]).image 
 data=base64.b64decode(binaryData)
        file="marketplaces/rakuten_ftp/static/imageToSave_"+str(p_id)+".png"
        with open(file, "wb") as imgFile:
            imgFile.write(data)

以上代码是从二进制数据创建文件但我未能在 mimetype 基础上应用条件。因为当我使用 Products id 查询 ir_attachment 表时,它返回 False。

for p_id in p_ids:
 attachments = self.env['ir.attachment']
 mimetype=attachments.search([('res_id','=',p_id)])

我正在考虑将 res_id 作为产品 id。但是 odoo 未能找到任何针对该 id 的记录。因此,如果有人知道我如何获得针对我的产品 id 的 mimetype,请帮助我。

【问题讨论】:

  • 您可以将图像数据保存为 png,天气它最初是 png 或 jpg with open('a.png', "wb") as imgFile: imgFile.write(data) 如果数据是图像数据(无论是 png 还是 jpg),它将起作用

标签: python odoo odoo-11


【解决方案1】:

您的代码看起来不错!但根据ir.attachement 对象,二进制数据存储在datas 字段中。因此,您可以使用该数据将二进制数据解码为图像文件!

已经在 Odoo v11 中尝试了下面的代码...它的工作原理是从存储在 datas 字段中的二进制数据创建的新图像文件!

product_image = self.env['ir.attachment']
product_images = product_image.search([('id', 'in', p_ids)])
for rec in product_images:
    with open("imageToSave.jpg", "wb") as imgFile:
        imgFile.write(base64.b64decode(rec.datas))

您还可以为mimetype 添加条件,因为p_ids 可以包含多个id,因此只取那些具有mimetypeimage/jpegimage/png 的id

编辑 #1

下面的代码 sn-p 已经用Odoo v11.0检查过

import base64
from odoo.tools.mimetypes import guess_mimetype


p_ids = [16, 18, 11, 38, 39, 40]  # Taking random ids of product.template
produtc_templates = self.env['product.template']
for p_id in p_ids:
    binary_data = produtc_templates.search([('id', '=', p_id)]).image
    mimetype = guess_mimetype(base64.b64decode(binary_data))
    file_path = ""
    if mimetype == 'image/png':
        file_path = "/home/Downloads/" + str(p_id) + ".png"
    elif mimetype == 'image/jpeg':
        file_path = "/home/Downloads/" + str(p_id) + ".jpeg"

    if file_path:
        with open(file_path, "wb") as imgFile:
            imgFile.write(base64.b64decode(binary_data))

【讨论】:

  • 感谢您的帮助。但现在我无法在 rakuten_ftp/static/imageToSave.png 这个位置找到我的图片。
  • @HassanALi 根据该代码 sn-p,如果我们没有指定路径,那么它将保存在 odoo 的同一目录中。如果你想在不同的地方新创建的文件,然后指定路径.....with open("/home/flash/Downloads/imageToSave.jpg", "wb") as imgFile: 这个文件将在Downloads 目录中创建!
  • 我如何获得 mimetype 因为我就是这样 image = produtc_templates.search([('id', '=',p_id)]).image 查找二进制图像但使用相同的方法我可以找到模仿类型。
  • 你可以在ir.attachment对象中找到mimetype字段!!您可以像这样修改您的域...product_images = product_image.search([('id', 'in', p_ids), '|', ('mimetype', '=', 'image/jpeg'), ('mimetype', '=', 'image/png')])
  • product_images = product_image.search([('id', 'in', p_ids) 针对 id 它返回不同的记录。针对 res_id 它返回 False 。
【解决方案2】:

产品图片不会保存为ir.attachment 的实例/记录。好吧,也许这已经改变了,但我没有这么快找到任何东西。

你能做的,就是使用ir.attachment的方法_compute_mimetype()

以下示例未经过测试:

def get_mimetype_of_product_image(self, product_id)
    product = self.env['product.product'].browse(product_id)
    mimetype = self.env['ir.attachment']._compute_mimetype(
        {'values': product.image})
    return mimetype

【讨论】:

  • 它是返回应用程序/八字节.. 它必须返回 png/jpg
  • 那么 Odoo 的“guess mimetype”功能就猜不出好的 enogh :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 2014-04-30
  • 2010-09-16
  • 1970-01-01
  • 2012-06-26
  • 2010-11-15
相关资源
最近更新 更多