如何用数据框中的对象(PIL图像)替换int值?

时间:2020-09-28 16:43:23

标签: python pandas matplotlib python-imaging-library

我想以网格样式绘制4个不同的图像,随机分布在144个图块(9x16)上。为此,我创建了一个具有144个值的随机矩阵(每个值都可以取数字0-3)。然后,我将这些值传输到数据帧,并用4张不同的图像替换了这些值。之后,我通过子图和imshow绘制图像。

matrix = np.random.randint(low=0, high=3, size=(16, 9))
df = pd.DataFrame(matrix).astype('object')

filenames = [['test1.jpg', 'test2.jpg'],
             ['test3.jpg', 'test4.jpg']]
images = [[Image.open(fn) for fn in row] for row in filenames]
dfIm = pd.DataFrame(images)

df = df.replace([0],dfIm.iloc[0,0])
df = df.replace([1],dfIm.iloc[0,1])
df = df.replace([2],dfIm.iloc[1,1])
df = df.replace([3],dfIm.iloc[1,0])

但是用PIL图像替换数据框中的数字会引发AssertionError。有谁知道,如何解决我的问题?谢谢!

0 个答案:

没有答案