如何修复 Python 中 PIL 的 "must be passed as a "sequence"" 警告?

How to fix "must be passed as a "sequence"" warning for PIL in Python?

我在 python 中使用 PIL 从 垂直和水平组合图像。但是,我收到警告:

/home/ceren/Documents/Python/combine_image.py:17: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
  imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )

我是 python 初学者,所以我不太明白如何解决这个问题。我认为代码中的 [f2, f4, f5] 已经是一个列表。然而,警告说它不是??我知道修复它不是必需的,但学习如何修复它会很好。

我应该在我的代码中更改什么,以便我不会再收到该警告?

我的代码如下:

内部主要:

fcom1 = 'Image.png'
ci.combine_horizontal([f2, f4, f5], fcom1, date)

在图像合成函数中:

def combine_horizontal(f, fcom, date):
    list_im = f
    imgs    = [ PIL.Image.open(i) for i in list_im ]
    min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
    imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs )    
    imgs_comb = PIL.Image.fromarray( imgs_comb)
    imgs_comb.save(fcom)

警告消息准确地告诉您是哪一行导致了问题,以及原因。

np.hstack 不(或不会)接受生成器表达式作为输入。以下行包含一个生成器表达式:

np.hstack(np.asarray( i.resize(min_shape)) for i in imgs)

您可以通过在列表理解中使用生成器来解决问题:

np.hstack([np.asarray( i.resize(min_shape)) for i in imgs])

或者把它包在一个元组中(这将是我个人的审美偏好:

np.hstack(tuple(np.asarray( i.resize(min_shape)) for i in imgs))