[英]Python: AttributeError while reading images with PIL library
I am trying to work on a set of images using PIL library. 我正在尝试使用PIL库处理一组图像。 I have no problems with importing images, but then, when I try to access size information of the first image from the list I receive an error. 我在导入图像方面没有任何问题,但是,当我尝试从列表中访问第一个图像的尺寸信息时,出现错误。 Function: 功能:
def loadImages(path):
image_path = listdir(path)
image_list = []
for img in image_path:
image = Image.open(path + img)
image_list.append(image)
return image_list
path = 'path/to/images'
images = loadImages(path)
N = len(images)
print("Number of images:", N)
w,h= Image.open(images[0]).size
And the whole error list: 以及整个错误列表:
File "<ipython-input-42-502dda9bf243>", line 1, in <module>
runfile('C:/Users/user/Desktop/mosaicing/vignetting/alphatrim_vs_distance.py', wdir='C:/Users/user/Desktop/mosaicing/vignetting')
File "C:\Users\user\anaconda\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\user\anaconda\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/user/Desktop/mosaicing/vignetting/alphatrim_vs_distance.py", line 39, in <module>
w,h= Image.open(images[0]).size
File "C:\Users\user\anaconda\Anaconda3\lib\site-packages\PIL\Image.py", line 2557, in open
prefix = fp.read(16)
AttributeError: 'MpoImageFile' object has no attribute 'read'
I am working on: pillow 5.0.0 python 3.6.4 我正在研究:枕头5.0.0 python 3.6.4
Looks like you are trying to open the image twice. 似乎您正在尝试打开图像两次。 Once in loadImage
and then you try to open what Image.open
returns (which is already an image) later when you do: 一旦进入loadImage
,然后尝试在执行以下操作时打开Image.open
返回的内容(已经是图像):
w,h = Image.open(images[0]).size
Because images[0] = Image.open(path + listdir(path)[0])
, you are effectively doing the following with the above line: 由于images[0] = Image.open(path + listdir(path)[0])
,因此您可以在上述行中有效地执行以下操作:
w,h = Image.open(Image.open(path + listdir(path)[0])).size
You should just be able to do: 您应该能够:
w, h = images[0].size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.