[GIS] GDAL C++ API: How to create PNG or JPEG from scratch

apigdal

I'm new to GIS and GDAL. My question probably is very basic, but I couldn't find answer. May be I don't understand GDAL ideology.

I need to create raster images from scratch, for example, JPEG or PNG. Their drivers don't support Create function – only CreateCopy. What is the common technique of new files creation in this case?

In principle, I can try to create Tiff because its driver suports Create(). Next, I can use CreateCopy() for PNG or JPEG using this Tiff. But such method looks indirect and unnatural for me. Also I suppose that this procedure can be too memory hungry if rasters are large.

I dealt with some image libraries before, they usually provide direct and simple way of bitmaps creation. Could somebody show me right direction for GDAL?

Best Answer

As user30184 said, in Python, the process would be creating a memory raster of the same dimensions (layers and layer extension), and executing the CreateCopy after that:

driver = gdal.GetDriverByName( 'MEM' )
driver2 = gdal.GetDriverByName( 'PNG' )
ds = driver.Create( '', 255, 255, 1, gdal.GDT_Int32)
ds2 = driver.CreateCopy('/tmp/out.png', ds, 0)

Then, just work with ds2

This should be translated to C++, of course. You can take a look at the Using CreateCopy() section in the tutorial: http://www.gdal.org/gdal_tutorial.html

Related Question