Export Data arcpy command line to save Tiff raster with zero compression

4636
10
07-30-2018 09:49 AM
SondraRobinson
New Contributor II

Hi guys. I have a script which cycles through a directory and adds 4band data. It works like a charm. The problem is that the customer wants the results UNcompressed. By default, all of the resulting files are compressed and so I have to manually open each file and then simply export data/raster to raster/compression set to zero. That works, but it's tedious and unnecessarily expensive in time.

I have looked everywhere I can to find out how to do the export process with zero compression by script. No luck, so I figured I'd try here.

Below is the currently functioning script.

----------------------------------------------------

import os,arcpy,glob

rasterpath = r"filepath-source"
outFolder = r"filepath-end"

arcpy.env.workspace = rasterpath

for rasterFile in arcpy.ListRasters("*.tif"):
    oName, oExt = os.path.splitext(rasterFile)
    outRaster = os.path.join(outFolder, oName+ ".tif")
    arcpy.CopyRaster_management(rasterFile,outRaster,"DEFAULTS","255","255","","","8_BIT_UNSIGNED")

-------------------------------------------------------

I know that the process of adding the band data creates pyramids, but my attempts to add zero compression to the environment on this script fails. What I'm looking for help with:

* the command line which will simply resave the data as-is but with no compression.

Compression is not an option for arcpy.CopyRaster_management in tiff format that I can find. There has to be a way to automate this. I'll be shooting myself if I have to manually open and resave hundreds of ortho images per object just for that single variable. I hope you guys can help! Thanks.

10 Replies
DanPatterson_Retired
MVP Emeritus

Compression (Environment setting)—Geoprocessing | ArcGIS Desktop 

Is an environment setting, so I suppose you tried setting it to None

compression_type (Required)  NONE—No compression will occur.

SondraRobinson
New Contributor II

I did try that:

arcpy.env.compression = "NONE"

The results are still compressed with LZW.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Copy Raster—Data Management toolbox | ArcGIS Desktop 

well it is supposed to honor Compression

out_rasterdataset   When storing a raster dataset to a JPEG file, JPEG 2000 file, TIFF file, or geodatabase, you can specify a compression type and compression quality.

HOWEVER!

Raster file formats—ArcGIS Pro | ArcGIS Desktop 

Depends on your tif

1-bit unsigned integer

No

CCITT (1D), CCITT Group 3, CCITT Group 4, Packbits, LZW, or None

Yes

4-bit signed integer

No

Packbits, LZW, or None

Yes

8-bit unsigned integer

Yes

Packbits, LZW, JPEG (lossy), or None

Yes

Big no for

8-bit signed integer

No

LZW

No

16-bit unsigned integer

Yes

LZW

Yes

16-bit signed integer

No

LZW

No

32-bit unsigned/signed integer, or floating point

No

LZW

No

0 Kudos
SondraRobinson
New Contributor II

Do you know how I would set that? I've seen that, but there are no examples I can copy.

I'm changing this line:

arcpy.CopyRaster_management(rasterFile,outRaster,"DEFAULTS","255","255","","","8_BIT_UNSIGNED")

to:

arcpy.CopyRaster_management(rasterFile,outRaster\\none,"DEFAULTS","255","255","","","8_BIT_UNSIGNED")

I'm using the 8-bit unsigned, so that appears to have the option at least.

8-bit unsigned integer

Yes

Packbits, LZW, JPEG (lossy), or None

0 Kudos
SondraRobinson
New Contributor II

Thanks for the suggestions Dan! I'll keep looking.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Sondra, you set the environment parameter as you had before within the script before copyraster

I ran a test to check to see if it did anything.  If it isn't set it defaults to 'LZ77' but I don't trust that capitalized NONE thing

arcpy.env.compression = None
arcpy.env['compression'] 'LZ77'
arcpy.env.compression = "NONE"
arcpy.env['compression']
'NONE'  # returned value
0 Kudos
DanPatterson_Retired
MVP Emeritus

Any luck?

0 Kudos
SondraRobinson
New Contributor II

Nope. none at all. still looking to save a raster without compression.

I did do a verification through print(arcpy.env.compression) that the compression environment was successfully set to None, both before and after the loop. It was came back with the correct setting, but still getting the compression.

Interestingly, the compression is actually LZW, not the default LZ77. I found a reference to "How To: Change the default compression format for output TIFF files" but the file this article mentions doesn't exist on the machine I'm using. https://support.esri.com/en/technical-article/000007588

0 Kudos
ThomasEmge
Esri Contributor

This seems to work ok in 10.6.1 and Pro 2.2

import arcpy
outFolder = r"C:\temp\data\results"
arcpy.env.workspace = r"c:\temp\data"
arcpy.env.compression = "NONE"
for rasterFile in arcpy.ListRasters("*", "TIF"):
 outName, outExtension = arcpy.os.path.splitext(rasterFile)
 outRaster = arcpy.os.path.join(outFolder, ''.join([outName, "_u", outExtension]))
 arcpy.CopyRaster_management(rasterFile, outRaster)
0 Kudos