如何从python中获取TIFF图像

今天我遇到了麻烦,因为我第一次使用TIFF文件,我有一个错误.我正试图在欧洲抓住一个带有污染物价值的光栅,所以我对保持图像的高分辨率并不感兴趣,但只是为了保存数据,我可以操纵图像.我的代码非常简单:

from __future__ import print_function
import numpy
import urllib2
from PIL import Image

f = open('./maccrasters/prova.tif','w')

fullMap = urllib2.urlopen("http://wdc.dlr.de/wdcservices/wcs.php?COVERAGE=17e72d93-76d9-4af6-9899-b7b04e2763c8&service=wcs&version=1.0.0&crs=epsg:4326&bbox=-25,30,45,70&RESX=0.1&RESY=0.1&request=getcoverage&format=application/x-tiff-32f&TIME=2015-12-13T00&elevation=0&OUTPUTFILENAME=17e72d93-76d9-4af6-9899-b7b04e2763c8_2015-12-13T00_0")

im = Image.open(fullMap) 

print(im.format, im.size, im.mode) # output is TIFF (700,400) F
im.show()
im.save(f, "TIFF")

系统返回一个输出,我找不到该错误的解决方案:

_TIFFVSetField: ./maccrasters/prova.tif: Invalid tag "TileOffsets" (not supported by codec).
Traceback (most recent call last):
  File "getrasters.py", line 20 in <module>
    im.save(f, "TIFF")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1665, in save
  save_handler(self, fp, filename)
File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 1307, in _save
  e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
File "C:\Python27\lib\site-packages\PIL\Image.py",line 430, in _getencoder
  return encoder(mode, *args + extra)

RuntimeError: Error setting from dictionary

有人可以帮帮我吗?

最佳答案 的GeoTIFF

您的文件不是常规的tiff文件,它是一个需要特殊库的geoTiff文件.

对于python,有georasters库来读取这些文件.然后,您可以使用matplotlib显示它们.

在我看来,使用请求有一个比urllib更好的界面:

import requests
from PIL import Image
import georasters as gr
import matplotlib.pyplot as plt


url = 'http://wdc.dlr.de/wdcservices/wcs.php'

query = {
    'COVERAGE': '17e72d93-76d9-4af6-9899-b7b04e2763c8',
    'service': 'wcs',
    'version': '1.0.0',
    'crs': 'epsg:4326',
    'bbox': '-25,30,45,70',
    'RESX': '0.1',
    'RESY': '0.1',
    'request': 'getcoverage',
    'format': 'application/x-tiff-32f',
    'TIME': '2015-12-13T00',
    'elevation': '0',
    'OUTPUTFILENAME': '17e72d93-76d9-4af6-9899-b7b04e2763c8_2015-12-13T00_0'
}

with open('test.tiff', 'wb') as f:

    ret = requests.get(url, stream=True, params=query)
    for data in ret.iter_content(1024):
        f.write(data)

data = gr.from_file('test.tiff')

plt.imshow(data.raster, cmap='gray')
plt.show()

结果:
《如何从python中获取TIFF图像》

点赞