类型错误:“图像”对象不支持项目分配

1983 年

我正在运行下面的模型,在拆分图像并尝试重新分配图像后,我收到错误消息说明。

    sample[:, :, 0] = 0
TypeError: 'Image' object does not support item assignment

我尝试了不同的方法来将图像分配回样本,但这没有帮助。任何帮助将不胜感激。非常感谢

这是我用来加载数据的代码:

import os

import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms, utils
from torchvision.transforms import functional
#from torchvision.transforms import Grayscalei
import pandas as pd
import pdb
import cv2
from cv2 import imread
from cv2 import resize

class CellsDataset(Dataset):
    # a very simple dataset

    def __init__(self, root_dir, transform=None, return_filenames=False):
        self.root = root_dir
        self.transform = transform
        self.return_filenames = return_filenames
        self.files = [os.path.join(self.root,filename) for filename in os.listdir(self.root)]
        self.files = [path for path in self.files
                      if os.path.isfile(path) and os.path.splitext(path)[1]=='.png']

    def __len__(self):
        return len(self.files)

        path = self.files[idx]
        sample = Image.open(path)
        sample = np.asarray(sample, dtype=np.uint8)
        sample = cv2.resize(src=sample, dsize=(1024, 1024))
        sample = functional.to_grayscale(sample, num_output_channels=3)

        sample[:, :, 0] = 0
        sample[:, :, 1] = 0

        if self.transform:
            sample = self.transform(sample)

        if self.return_filenames:
            return sample, path
        else:
            return sample

    def duplicate_and_transform(self,transforms):
        currfilelen = self.__len__()
        
        for pind in range(0,currfilelen-1):
            path = self.files[pind]
            sample = Image.open(path)
            samplet = transforms(sample)
            # save the transformed images and save the associated counts:
            newpath = path[:-4]+'trans.png' #path[:-4] is the path without the .png extension.
            samplet.save(newpath,"PNG")
            metadata = pd.read_csv(os.path.join(self.root,'gt_red.csv'))
            metadata.set_index('filename',inplace=True)
            count = metadata['count'][os.path.split(path)[-1]]
            samplet.save(newpath,"PNG")
            f = open(self.root+'gt.csv','a')
            basepath = os.path.basename(path) # strip away the directory list and just get the filename
            f.write(basepath[:-4]+'trans.png,'+str(count)+'\n')
            # return the new file names and add to self.files.
            self.files.append(newpath)
            #pdb.set_trace()
WingedRasengan927

您直接对 PIL 对象进行操作。您必须先将其转换为 numpy 数组,然后再操作这些值:

sample = Image.open(path)
sample = np.asarray(sample, dtype=np.uint8)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

对象不支持项目分配错误

类型错误:“元组”对象不支持非元组对象上的项目分配

列表对象上的“类型错误:‘元组’对象不支持项目分配”

Python TypeError:“类型”对象不支持项目分配

TypeError:“类型”对象不支持项目分配

类型错误:“str”对象不支持迭代中的项目分配

类型错误:“str”对象不支持项目分配熊猫添加列

TypeError:“ int”对象不支持项目分配错误

错误:“str”对象不支持项目分配

错误:“浮动”对象不支持项目分配

数组操作:错误:“int”对象不支持项目分配

错误:“int”对象不支持项目分配

'tuple'对象不支持项目分配

“'str'对象不支持项目分配”

“方法”对象不支持项目分配

类型错误:“ builtin_function_or_method”对象不支持项目分配。我该如何解决?

Lambda错误“ TypeError:'str'对象不支持项目分配回溯”

错误:在 Zapier 中进行 HTTP 调用时,“NoneType”对象不支持项目分配

错误“ numpy.int32”对象不支持项目分配

在Featuretools中创建实体集错误TypeError:'str'对象不支持项目分配

当Databricks / Spark DataFrame'对象中的执行代码不支持项目分配时收到错误

python中数据帧的简单日志:错误:'type'对象不支持项目分配

Python,累加“ itertools.accumulate”对象不支持项目分配错误

从 Python3 获取“'str'对象不支持项目分配”错误

TypeError /数组索引;'int'对象不支持项目分配

Python Azure Databrick:“ DataFrame”对象不支持项目分配

Python 脚本... TypeError: 'method' 对象不支持项目分配

bluedata mlops 预测 - “NoneType”对象不支持项目分配

TypeError:“ NodeView”对象不支持项目分配-NetworkX