[gdal-dev] Clipping an image via Python API problems

Graeme Merrall graeme at inetix.com.au
Mon Nov 28 01:05:23 EST 2011


I've discovered that he bands are determined by the args to driver.Create()
that is if I create a 4 band image then the bands are correct, however if
I'm going to copy the image chunk around using ReadAsArray and WriteArray,
doesn't it mean I need to retreive each band in turn?
Or have I missed something and there's an easier way to extract a portion
of an image and write it out?

Cheers,
 Graeme

On Mon, Nov 28, 2011 at 3:40 PM, Chaitanya kumar CH
<chaitanya.ch at gmail.com>wrote:

> Graeme,
>
> You do need to copy all bands. Also, make sure you set the correct number
> of bands while creating the destination dataset.
>
> Color interpretation is useful when you have multiple bands. If you want
> to show your image in red, set it to GCI_PaletteIndex and set a colortable.
>
> On Mon, Nov 28, 2011 at 6:40 AM, Graeme Merrall <graeme at inetix.com.au>wrote:
>
>> The smallest almost working code chunk I have right now is below.  This
>> is taaking a 40k by 40k and pulling out the top left quarter.  The original
>> has 4 bands - RGBA
>>
>> from osgeo import gdal
>> import numpy
>> img = gdal.Open("original.tif")
>> geotransform = img.GetGeoTransform()
>> band1 = img.GetRasterBand(1)
>>
>> driver = gdal.GetDriverByName("GTiff)
>> # coordinates will be calculated but hardcoded for now
>> dst = driver.Create('sliver.tif', 20000, 20000, 1, gdal.GDT_Byte)
>> dst.SetProjection(img.GetProjection())
>> scanline1 = band1.ReadAsArray(0, 0,  20000, 20000)
>> dst.GetRasterBand(1).WriteArray(scanline1)
>>
>> Should I be looping through each band and reading/writing to the image?
>> I'm assuming yes
>>
>> Also. band 1 of the original image is red but in the new image the band
>> color is grey. I've tried a few variations with
>> GDALRasterBand.SetColorInterpretation() with no luck.  What have I missed?
>>
>> Cheers,
>>
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>>
>
>
>
> --
> Best regards,
> Chaitanya kumar CH.
>
> +91-9494447584
> 17.2416N 80.1426E
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20111128/91ef5188/attachment.html


More information about the gdal-dev mailing list