0

我正在尝试从 URL 读取图像(由 Google 的静态地图 API 提供)。

图像在浏览器中显示正常。

在此处输入图像描述

https://maps.googleapis.com/maps/api/staticmap?maptype=satellite¢er=37.530101,38.600062&zoom=14&size=256x278&key= ...

但是当我尝试使用 misc.imread 将它加载到数组中时,它似乎最终成为一个二维数组(即扁平化,没有 RGB 颜色)。

这是我正在使用的代码(我隐藏了我的 API 密钥):

from scipy import ndimage
from scipy import misc
import urllib2
import cStringIO

url = \
    "https://maps.googleapis.com/maps/api/staticmap?maptype=satellite&" \
    "center=37.530101,38.600062&" \
    "zoom=14&" \
    "size=256x278&" \
    "key=...."

file = cStringIO.StringIO(urllib2.urlopen(url).read())
image = misc.imread(file)
print image.shape

(278, 256)

我所期望的是一个 3-d 形状数组 (278, 256, 3)。

也许它没有正确读取文件?

In [29]:
file.read()[:30]
Out[29]:
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x00\x00\x00\x01\x16\x08\x03\x00\x00\x00\xbe'
4

1 回答 1

2

\x03后面的字节\x08表示您的文件是RGB索引的(即它有一个调色板)。scipy.misc.imread读取索引的 PNG 文件时会发生错误。返回的数组是索引值数组,而不是实际的 RGB 颜色。该错误已针对 scipy 0.17.0 进行了修复,但尚未发布。

一种解决方法是scipy.ndimage.imread与选项一起使用mode='RGB'

(由于历史原因,存在两个略有不同imread的功能。在这种情况下,一个有选项的事实证明是有帮助的。实现在 scipy 0.17.0 中统一。)mode

于 2016-02-13T07:21:51.993 回答