[Gdal-dev] problem converting to jpeg

Frank Warmerdam warmerdam at pobox.com
Wed May 28 12:31:35 EDT 2003


Frank Warmerdam wrote:
> Tim Smith wrote:
> 
>> I am trying to convert a NITF file into a JPEG file.
>> gdalinfo shows that the NITF has a
>> colorInterp=palette.
>> When I use gdal_translate (gdal_translate -of JPEG
>> /path/file.nitf /path/file.jpeg), the created jpeg
>> file has a colorInterp=Grey.  Is there a way to do
>> this without the loss of color?
> 
> 
> Tim,
> 
> JPEG does not support paletted output files, and gdal_translate does not
> currently support converting 1 band paletted images into 3 band RGB images.
> So currently gdal_translate cannot convert the file to JPEG in a useful 
> way.
> Do you really need it in JPEG?  It should convert easily to PNG, 
> GeoTIFF, etc.

Tim,

In followup, I have written a Python script (pct2rgb.py) that is provides
a service to translate pseudocolored files into RGB or RGBA.  The script
is in CVS in gdal/pymod.  If you are interested in pursuing this you can
rebuild from CVS (ensuring you have the python Numeric package installed)
and then do something like:

  gdal/pymod/pct2rgb.py -of JPEG overview.ovr overview.jpg

This script is a good example of applying lookup tables in python using
the "take" operation.  The portion that applies the color table looks like
this:

# ----------------------------------------------------------------------------
# Build color table.

lookup = [ Numeric.arrayrange(256),
            Numeric.arrayrange(256),
            Numeric.arrayrange(256),
            Numeric.ones(256)*255 ]

ct = src_band.GetRasterColorTable()

if ct is not None:
     for i in range(min(256,ct.GetCount())):
         entry = ct.GetColorEntry(i)
         for c in range(4):
             lookup[c][i] = entry[c]

...

# ----------------------------------------------------------------------------
# Do the processing one scanline at a time.

for iY in range(src_ds.RasterYSize):
     src_data = src_band.ReadAsArray(0,iY,src_ds.RasterXSize,1)

     for iBand in range(out_bands):
         band_lookup = lookup[iBand]

         dst_data = Numeric.take(band_lookup,src_data)
         tif_ds.GetRasterBand(iBand+1).WriteArray(dst_data,0,iY)


Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent





More information about the Gdal-dev mailing list