【问题标题】:Google Colab - AttributeError: 'numpy.ndarray' object has no attribute 'seek' and 'read'Google Colab - AttributeError:“numpy.ndarray”对象没有属性“seek”和“read”
【发布时间】: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,但错误仍然出现,我该怎么办?

【问题讨论】:

  • rgbtirqb 中的每一个都是 numpy 数组(这就是 imread() 返回的内容)。但是您正在尝试使用 PIL 来“打开”这些 numpy 数组。相反,您希望 PIL 打开文件。所以传递 PIL 文件名而不是数组,或者不使用 PIL。

标签: python-3.x opencv image-processing python-imaging-library google-colaboratory


【解决方案1】:

不使用 pil 怎么样,好像 opencv 可以随心所欲

import matplotlib.pyplot as plt
import numpy as np
import sys
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]

# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
min_width = min([img.shape[1] for img in list_im])
min_height = min([img.shape[0] for img in list_im])
list_img = [cv2.resize(img, (min_width, min_height)) for img in list_im]

imgs_comb = np.hstack( list_img )

# save that beautiful picture
cv2.imwrite('result.jpg', imgs_comb)

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2021-05-24
    • 2021-11-24
    • 2020-12-03
    • 2020-11-29
    • 2020-10-06
    • 2018-01-25
    • 2016-06-29
    • 2020-03-25
    相关资源
    最近更新 更多