5

I am trying to change the pixel value of a tif file (containing projection information) but after the pixel value is changed it is losing projection information.

I am using the Python imaging library for this and the code is like this

import Image
im = Image.open("some tif file")
im.putpixel((2,3),0)

So please help me in solving this ..

1 Answer 1

6

You need a Python library that supports the GeoTIFF format. Libraries that support only the TIFF file discard the projection and geotransform metadata when they are saved. GDAL is the most used open source Python library for reading/writing spatially aware rasters. So, for your example:

from osgeo import gdal

# Load file, and access the band and get a NumPy array
src = gdal.Open('myfile.tif', gdal.GA_Update)
band = src.GetRasterBand(1)
ar = band.ReadAsArray()

# Assign a new raster pixel value
ar[2,3] = 0

# Save/close the file
band.WriteArray(ar)
del ar, band, src
2
  • Thanks Mike ... I will you please suggest me some good resource where i can get the gdal related stuffs for Python.. Because I know C++ stuffs for gdal but i get some problems in converting that to the python
    – Vicky
    Nov 14, 2011 at 6:41
  • 1
    Installing Python bindings for GDAL is simple for most Linux distros. For Windows, there are a few with my personal favourite installer being a simple installer from Christoph Gohlke
    – Mike T
    Nov 14, 2011 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.