4

Today I'm in trouble because for the first time I have to work with TIFF files, and I have an error. I'm trying to grab a raster with the values of pollution agents in Europe, so I'm not interesting to keep an high level of resolution of the image, but only to keep data, I can manipulate the image. My code is very simple:

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")

The system returns me an output and I can't find a solution for that error:

_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

Someone can help me?

Lupanoide
  • 3,132
  • 20
  • 36
  • 1
    Does this help ? http://stackoverflow.com/questions/7569553/working-with-tiffs-import-export-in-python-using-numpy – Marco Dec 15 '15 at 14:29
  • @Marco , Unfortunately not, I can't visualize in the right way the image with the .show() method. It's not only a problem for save the image – Lupanoide Dec 15 '15 at 14:33

2 Answers2

2

geoTiff

Your file is not a regular tiff-file, it's a geoTiff file which needs a special library.

For python there is the georasters library to read those files. You can then show them with matplotlib.

Using requests has a way nicer interface than urllib in my opinion:

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()

Result: result

MaxNoe
  • 14,470
  • 3
  • 41
  • 46
  • Thanks for your answer but for me doesn't works well: I don't know why, I download it but I visualize only a part of the image - only the northen europe, not entirely, such as a tile – Lupanoide Dec 15 '15 at 14:50
  • That's a problem of the image, not of the code. I tested it with this tiff: http://imgsrc.hubblesite.org/hu/db/images/hs-2006-01-a-hires_tif.tif and it works fine. – MaxNoe Dec 15 '15 at 14:54
  • No, the problem is in python, and how open it. If I download this image manually, putting the url in a browser, I can visualize it well, but the file is saved as .php file – Lupanoide Dec 15 '15 at 15:01
  • Calling im.show() even throws an error with python3, no error but missing parts with python2. – MaxNoe Dec 15 '15 at 15:02
  • How do you open it when you dowloaded it with the browser? I cannot. – MaxNoe Dec 15 '15 at 15:09
  • Ah, so you have a geoTiff file, not a normal tiff file? I figured it out i think, I updated the answer. – MaxNoe Dec 15 '15 at 15:51
  • 1
    Thanks a lot Max! Downloading this files is very important for my project and without you I can't solve this problem. You are my hero today! – Lupanoide Dec 15 '15 at 17:12
  • You also might want to have a look into `cartopy` for plotting stuff on maps. – MaxNoe Dec 15 '15 at 17:28
0

I've worked too with .tiff images

What I used was imread from openCV and it worked really well

MaTh
  • 217
  • 1
  • 12