Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 ..

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

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
share|improve this answer
    
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 '11 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 '11 at 8:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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