从odoo中上传图像文件获取属性

Getting attributes from uploading image file in odoo

在自定义模块中,我正在将图像文件上传到数据库。现在我想从上传文件中获取一些属性,同时图像文件正在上传到 odoo 并将这些属性保存在数据库中。要获取文件名,我在 form.view:

中使用以下代码
<field name="image" filename="name"/>

使用参数文件名="name",图像文件的文件名被保存到数据库字段名中。这很好用。

我现在的问题是,如何从上传的图片文件中获取其他属性?例如我想获取录制日期、分辨率、图像大小...有没有人知道如何从上传图像文件中获取这些属性以将这些信息保存在数据库记录中?

非常感谢!

您可以继承 "ir.attachment" 并创建您的元数据字段并将此详细信息存储在模型中,您可以尝试通过此代码查找详细信息。

图像文件以编码格式获取,因此您可以解码 64 抛出转换文件并获取图像并获取图像的元数据。

import os
import time
from stat import *  # ST_SIZE etc

try:
    st = os.stat('1.png')
except IOError:
    print "failed to get information about", file
else:
    print "file size:", st[ST_SIZE]
    print "file modified:", time.asctime(time.localtime(st[ST_MTIME]))

This参考Link帮助你....

可能是其他解决方案,但当时没有找到所以你可以试试这个代码..

类似于此的东西应该可以工作:

import PIL 
from StringIO import StringIO


class SomeModelWithImage(models.Model):
    image = fields.Image(required=True)
    name = fields.Char()
    size = fields.Integer(compute='_compute_image_details')
    camera_maker = fields.Char(compute='_compute_image_details')

    @api.one
    @api.depends('image')
    def _compute_image_details(self):
        image_content = self.image.decode('base64')

        # File size
        self.size = len(self.image_content)

        # Camera make and model from EXIF tags
        image = PIL.Image.open(StringIO(image_content))
        exif_tags = image._getexif()

        # 0x010f is a numeric code for the "make" exif field
        # You can find a list of fields here: exiv2.org/tags.html
        self.camera_maker = exif_tags.get(0x010f)

这会创建两个附加字段 - sizecamera_maker,它们会在设置 image 字段时自动填充。相机制造商信息取自图像的 EXIF 标签。您可以在 exiv2.org/tags.html 下看到其他可能的 EXIF 标签。您应该注意,图像没有任何标签的要求。