从特定文件打开图像?

时间:2017-05-05 16:49:36

标签: python numpy python-imaging-library glob

{{1}}

继续给我这个错误:

im = Image.open(filename)

文件" /home/ns3/.local/lib/python2.7/site-packages/PIL/Image.py" ;,第2416行,公开     fp = io.BytesIO(fp.read())

AttributeError:' list'对象没有属性'读取

如何从该特定路径打开图像并将图像用作数组I?

1 个答案:

答案 0 :(得分:1)

问题是glob.glob()返回一个列表(a possibly-empty list of path names that match pathname),你想要一个字符串。

所以要么插入[0]

import glob
import numpy as np
from PIL import Image

filenames = glob.glob('/home/ns3/PycharmProjects/untitled1/stego.pgm')
filename = filenames[0]
im= Image.open(filename)
(x,y) = im.size
I = np.array(im.getdata()).reshape(y, x)

或全部跳过全球

import numpy as np
from PIL import Image

filename = '/home/ns3/PycharmProjects/untitled1/stego.pgm'
im= Image.open(filename)
(x,y) = im.size
I = np.array(im.getdata()).reshape(y, x)