2

I'm working on a simple script using GDAL Driver.CreateCopy function, but I keep getting the same error. The script is very basic:

import os,arcpy,sys from osgeo import gdal source_folder=r'E:\to_conv' conv_folder='E:\conv' compression_type='JPEG' compression_quality='70' arcpy.env.workspace=source_folder rasters=arcpy.ListRasters("*","TIF") for raster in rasters: tiff_file=os.path.join(conv_folder,raster) source_file=os.path.join(source_folder,raster) print(source_file) try: dformat="GTiff" driver = gdal.GetDriverByName( dformat ) metadata = driver.GetMetadata() if metadata.has_key(gdal.DCAP_CREATE) and metadata[gdal.DCAP_CREATE] == 'YES': print 'Driver %s supports Create() method.' % dformat if metadata.has_key(gdal.DCAP_CREATECOPY) and metadata[gdal.DCAP_CREATECOPY] == 'YES': print 'Driver %s supports CreateCopy() method.' % dformat src_ds = gdal.Open(source_file) dst_ds = driver.CreateCopy(tiff_file,source_file, options=["ALPHA=YES"] ) except Exception: e=sys.exc_info()[1] print ("Error converting "+ source_file + " to " + tiff_file + ": " +str(e)) 

This is the error that I keep getting:

E:\to_conv\testfile1.tif Driver GTiff supports Create() method. Driver GTiff supports CreateCopy() method. Error converting E:\to_conv\testfile1.tif to E:\conv\testfile1.tif: in method 'Driver_CreateCopy', argument 3 of type 'GDALDatasetShadow *' 

Any help ?

10
  • What is the meaning of "0" in dst_ds = driver.CreateCopy(tiff_file,source_file, 0)? Commented Nov 13, 2019 at 9:59
  • Sorry I forgot to remove the ) after a test I did, the correct line is dst_ds = driver.CreateCopy(tiff_file,source_file, 0, [ "TILED=YES", "COMPRESS="+compression_type, "JPEG_QUALITY="+compression_quality] ) Commented Nov 13, 2019 at 10:51
  • and strictly speaking this is the bStrict parameter: TRUE if the copy must be strictly equivalent, or more normally FALSE indicating that the copy may adapt as needed for the output format. Commented Nov 13, 2019 at 10:55
  • The syntax used in GDAL autotests github.com/OSGeo/gdal/blob/master/autotest/gcore/tiff_write.py seems to be gdaltest.tiff_drv.CreateCopy('/vsimem/rgb_added_alpha.tif', ds, options=['ALPHA=YES']). Commented Nov 13, 2019 at 11:00
  • I changed the line to dst_ds = driver.CreateCopy(tiff_file,source_file, source= [ "TILED=YES", "COMPRESS="+compression_type, "JPEG_QUALITY="+compression_quality] ), same error Commented Nov 13, 2019 at 11:08

1 Answer 1

0

The second argument of CreateCopy(dstFile,srcDs,...) should be the Dataset Object instead of a filename.

So, in this example the source_file should be replaced by src_ds.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.