【发布时间】:2023-03-19 02:39:01
【问题描述】:
我正在尝试水平连接 3 张图像。我在互联网上找到了一种看起来足够好的方法,问题是我有两个错误,我无法弄清楚如何修复。
注意:我在 Python 3 中使用 Google Colab。
以下是错误:
AttributeError: 'numpy.ndarray' object has no attribute 'seek'
AttributeError: 'numpy.ndarray' object has no attribute 'read'
我的代码:
import matplotlib.pyplot as plt
import numpy as np
import sys
import PIL
import cv2
rgb = cv2.imread("/content/teste/LC08_L1TP_222063_20180702_20180716_01_T1_1_3.tif")
tir = cv2.imread("/content/teste/LC08_L1TP_222063_20180702_20180716_01_T1_TIR_1_3.tif")
qb = cv2.imread("/content/teste/LC08_L1TP_222063_20180702_20180716_01_T1_QB_1_3.tif")
list_im = [rgb, tir, qb]
imgs = [ PIL.Image.open(i) for i in list_im ]
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
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 ) )
# save that beautiful picture
imgs_comb = PIL.Image.fromarray( imgs_comb)
imgs_comb.save( 'Trifecta.jpg' )
显然错误发生在这一行:---> 12| imgs = [ PIL.Image.open(i) for i in list_im ]
因此我尝试将Pillow库更新到5.3.0,但错误仍然出现,我该怎么办?
【问题讨论】:
-
rgb、tir和qb中的每一个都是 numpy 数组(这就是imread()返回的内容)。但是您正在尝试使用 PIL 来“打开”这些 numpy 数组。相反,您希望 PIL 打开文件。所以传递 PIL 文件名而不是数组,或者不使用 PIL。
标签: python-3.x opencv image-processing python-imaging-library google-colaboratory