Monday, 26 December 2016

Editing .tiff rasters in Python


I am trying to edit .tif rasters in python, but I have some issues regarding the tiff to array conversion. I read there are multiple options for opening rasters (gdal, PIL, matplotlib), but which of those is the best for allowing me to convert to array, edit (apply local filters), then convert back to .tif?



For example, I get a strange result when opening arrays from a .tif raster using PIL library.


The code looks like this:


import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm
import numpy as np
import pywt as pw
import tkFileDialog as tk
from PIL import Image


def fct(fisier):
img = Image.open(fisier)
arr=np.array(img, dtype=np.float)
plt.imshow(arr, cmap=cm.Greys_r)
plt.show()
print(arr)
return arr, img
file = tk.askopenfile(initialdir='C:/temp', mode='rb')
fct(file)


But the array that is displayd in all white, and has this form:


[[ -2.14748365e+09  -2.14748365e+09  -2.14748365e+09 ...,   0.00000000e+00
0.00000000e+00 6.19762981e+08]

What do I need to do in order to open the tiff raster as an array, so I can edit it further?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...