2017-02-28 22 views
1

我与Web服务请求使用基于传递参数,以获得图像的工作。我得到的第一个响应是一个带有文件参考URL的XML模式。显示TIFF图像在使用Python不保存Juypyter笔记本/下载

<?xml version="1.0"?> 
<Coverages schemaLocation="http://localhost/server/schemas/wcs/1.1.1/wcsCoverages.xsd"> 
<Coverage> 
    <Title>Filename</Title> 
    <Abstract/> 
    <Identifier>Filename</Identifier> 
    <Reference href="http://localhost/server/temp/filename.tif"/> 
</Coverage> 
</Coverages> 

接下来使用xml.etree.ElementTree我提取了URL。接下来我需要的是dsiplay的Jupyter笔记本电脑,TIFF图像(或其他图像),而不下载(作为一个图像可以是有时超过50或100 MB)

目前我下载并绘制出文件读取和文件数据转换成阵列(如pyplot情节图像阵列/矩阵)后。

import requests as req # request wcs urls 
import xml.etree.ElementTree as ET # used for xml parsing 
import matplotlib.pyplot as plt # display image 
import gdal 

# Download the File to local directory using Chunks 
    chunk_size=1024 
    local_filename = url.split('/')[-1] # Filename from url 
    r = req.get(url, stream=True) 
    with open(local_filename, 'wb') as f: 
     for chunk in r.iter_content(chunk_size): 
      if chunk: 
       f.write(chunk) 

    # Read File Raster Data as Array using Gdal 
    gtif = gdal.Open(local_filename) 
    georaster = gtif.ReadAsArray() 

    # Plot image using matplotlib 
    plt.imshow(georaster) 
    plt.title(local_filename) 
    plt.show() 

那么,有没有办法将从请求API文件直接插入图像阵列的原始响应(以块或整体)和笔记本电脑显示它(无需下载和本地目录中取空间)

从TIFF文件GET请求的原始响应低于

resp2 = req.get('tiffFielurl') 
rawdata = resp2.content 
rawdata[0:10] 

Output: b'MM\x00*\x00\x00\x00\x08\x00\x10' 

我试图寻找这个问题,但没有发现任何很好的答案,所以如果有任何相关问题,或复制给我提供的链接。

+0

可以显示使用Matplotlib远程图像在这个类似的StackOverflow问题概述:【如何绘制(从HTTP URL)的远程图像(http://stackoverflow.com/questions/12116050/how -to-积远程图像从-HTTP-URL)。 – Brian

+0

@布赖恩好像你已经给正在与“PNG”文件链接中提到的解决方案只,我想请求TIFF文件,并得到以下错误。 “** ValueError:invalid PNG header **” –

+0

实际上,在尝试了许多不同的方法后,我无法找到任何能够很好地利用来自url的tiff文件(至少在我手中)的东西。您可以尝试使用io.BytesIO或np.fromstring或np.frombuffer。如果不知道更多关于图像数据(预期的数据类型和大小)并访问我可以复制的示例,我将无法进一步帮助您。祝你好运,让我知道,如果你发现任何工作。 – Brian

回答

0

这样做大量的研究和尝试不同的解决方案后,在我看来,做上述步骤即显示TIFF文件,现在唯一的办法就是下载和使用@中并转换成使用matplotlib排列显示读取数据。

如在下面的链接提到的解决方案仅接受“PNG”文件。

How to plot remote image (from http url)

whcih说到最后,我们需要,我也尝试和失败PIL库。

from PIL import Image 
resp2 = req.get('tiffFielurl') 
resp2.raw.decode_content = True 
im = Image.open(resp2.raw) 
im 

给出输出:

<PIL.TiffImagePlugin.TiffImageFile image mode=I;16BS size=4800x4800 at 0x11CB7C50> 

和PIL对象转换为numpy的阵列或者甚至从PIL对象获取数据或像素给出无法识别模式错误的错误。

im.getdata() 
im.getpixels((0,0)) 
numpy.array(im) 

所有给予同样的错误

257   if not self.im or\ 
    258   self.im.mode != self.mode or self.im.size != self.size: 
--> 259    self.im = Image.core.new(self.mode, self.size) 
    260   # create palette (optional) 
    261   if self.mode == "P": 

ValueError: unrecognized mode 

它配备的是PIL甚至不支持在PIL的TIFF文件对象定义上述16位有符号整数像素。

https://pillow.readthedocs.io/en/4.0.x/handbook/concepts.html#concept-modes