【发布时间】:2017-09-15 21:11:57
【问题描述】:
我正在使用 Python 和 Numpy 拍摄几张相同像素尺寸的图像并创建一个二维数组,因此数组的每一行代表一个图像,每一列将代表某个位置的像素。
为此,我已阅读图像文件并尝试使用 numpy.concatenate。代码是
#url of picture data
X_p = data.link
#list for storing the picture data
X= []
#read in the image from the url, and skip poster with 404 error
for url in X_p:
try:
loadimg = urllib.request.urlopen(url)
image_file = io.BytesIO(loadimg.read())
img = Image.open(image_file)
#Concatenate to linearize
X.append(np.concatenate(np.array(img)))
#404 error
except urllib.error.HTTPError as err:
if err.code == 404:
continue
else:
raise
#cast the list into numpy array
X = np.array(X)
#test to see if X is in correct dimension
print(X.shape)
我运行了这段代码,X 的形状每次都以这种格式出现
(图片数量,高X宽,3)
例如,如果我加载 12 个 200x200 像素的图像 url,结果是
(12, 40000, 3)
我需要的是把最后的 3 去掉,当我什至不明白 3 是从哪里来的时候很难。
我认为我遇到的问题是在错误的地方追加或连接。当我删除 np.concatenate 时,它只是显示(12、200、200、3)。
我已经在网上搜索了 numpy 图像处理和连接,但我没有遇到任何可以解释和解决正在发生的事情的东西。
我们不胜感激。提前感谢您花时间阅读这篇文章并回答..
【问题讨论】:
标签: python-3.x numpy image-processing multidimensional-array