python-3.x 如何将PIL.Jpegimageplugin.Jpegimagefile类型的图像存储到Odoo中

ifsvaxew  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(78)

我从Excel中检索数据,其中一列是一堆图像,主要是JPEG,我使用openpyxl和openpyxl_image_loader读取Excel和检索数据
我就是这样阅读我的数据的

def get_data_from_excel(file):
    data = defaultdict(list)  
    try:  
        wb = openpyxl.load_workbook(filename=BytesIO(base64.b64decode(file)))  
        ws = wb.active  
        image_loader = SheetImageLoader(ws)  
        # first row will be keys in dict  
        keys = [cell.value for cell in ws[1]]  
        i = 0
        for row in ws.iter_rows(min_row=2, max_col=ws.max_column, max_row=ws.max_row):  
            temp = {}  
            for key, cell in zip(keys, row):  
                if key == 'Photo':  
                    try:  
                        image = image_loader.get(cell.coordinate)  
                          
                        temp[key] = base64.b64decode(image.tobytes())
                        continue  
                    except Exception as e:  
                        print(e)  
                        temp[key] = False  
                        break           
                temp[key] = cell.value  
            data[i].append(temp)
            i+=1  
            
      
    except Exception as e:  
        raise ValidationError(_('Please upload a valid Excel file! ', e))  
    finally:  
        return data

字符串
通过id获取模型后,尝试将数据设置为字段

def set_photo(self, model, photo):  
    try:  
        model.image_1920 = photo   
    except Exception as e:  
        print(f'Error while setting photo for model {model}, error: {e}')


我得到的错误
无法将此文件解码为图像文件。请尝试使用其他文件。
我尝试使用Pillow open()函数,得到此异常
UnidentifiedImageError(“无法识别图像文件<_io.BytesIO object at 0x7f3ddcb0b2c0>”)
有关图像属性的更多详细信息
x1c 0d1x的数据
使用guess_mimetype

print(guess_mimetype(base64.b64decode(image.tobytes())))


输出
application/octet-stream
我将感激任何帮助或解释,提前谢谢

mrfwxfqh

mrfwxfqh1#

我设法让它工作,我不得不改变我如何转换JpegImageFile到base64

image = image_loader.get(cell.coordinate)
buffer = BytesIO()
image.save(buffer, format=image.format)
image_bytes = buffer.getvalue()
image_base64 = base64.b64encode(image_bytes)

字符串
最终代码

def get_data_from_excel(file):
    data = defaultdict(list)  
    try:  
        wb = openpyxl.load_workbook(filename=BytesIO(base64.b64decode(file)))  
        ws = wb.active  
        image_loader = SheetImageLoader(ws)  
        # first row will be keys in dict  
        keys = [cell.value for cell in ws[1]]  
        i = 0
        for row in ws.iter_rows(min_row=2, max_col=ws.max_column, max_row=ws.max_row):  
            temp = {}  
            for key, cell in zip(keys, row):  
                if key == 'Photo':  
                    try:  
                        image = image_loader.get(cell.coordinate)  
                        buffer = BytesIO()  
                        image.save(buffer, format=image.format)  
                        image_bytes = buffer.getvalue()  
                        image_base64 = base64.b64encode(image_bytes)  
                          
                        temp[key] = image_base64
                        continue  
                    except Exception as e:  
                        print(e)  
                        temp[key] = False  
                        break           
                temp[key] = cell.value  
            data[i].append(temp)
            i+=1  
            
      
    except Exception as e:  
        raise ValidationError(_('Please upload a valid Excel file! ', e))  
    finally:  
        return data


我不确定这是否能帮助到任何人,但我把我的答案留在这里,以防万一

相关问题