1

I need to convert TIFFs to BMPs using CopyRaster from ascii to raster output TIFFs. I am not sure how to define the "outRaster" so that it will convert the rasters to BMP. I'm guessing that the line of code which I denote below needs additional consideration.

import os,arcpy,glob

rasterpath = r"C:\VMshared\small_example_valley2\snowdepthout"
outFolder = r"C:\VMshared\small_example_valley2\snowrast"
rasterList = glob.glob(rasterpath + "/*.tif")

for rasterFile in rasterList:
    outRaster = os.path.join(outFolder, rasterFile + "bmp") #THIS LINE??
    arcpy.CopyRaster_management(rasterFile,outRaster,"#","-3.402823e    +038","NONE","NONE","8_BIT_UNSIGNED","ScalePixelValue","NONE")

***NOTE - The code below works to convert to jpg or bmp although I would guess some syntax is missing.

import os,arcpy,glob

arcpy.env.workspace = r"C:\VMshared\small_example_valley2\snowtest2"
outFolder = r"C:\VMshared\small_example_valley2\snowtest3"

for rasterFile in arcpy.ListRasters("*.tif"):

    oName, oExt = os.path.splitext(rasterFile + ".jpg")
    outRaster = os.path.join(outFolder, oName + ".jpg") 
    arcpy.CopyRaster_management(rasterFile,outRaster,"#","#","-9999","NONE","NONE","8_BIT_UNSIGNED","NONE","NONE")

1 Answer 1

5
outRaster = os.path.join(outFolder, rasterFile + "bmp") #THIS LINE??

should be

outRaster = os.path.join(outFolder, rasterFile + ".bmp") #THIS LINE??

instead of using glob perhaps use the inbuilt arcpy.ListRasters:

rasterpath = r"C:\VMshared\small_example_valley2\snowdepthout"
outFolder = r"C:\VMshared\small_example_valley2\snowrast"
arcpy.env.workspace = rasterpath 
for rasterFile in arcpy.ListRasters("*.tif"):
    oName, oExt = os.path.splitext(rasterFile)
    outRaster = os.path.join(outFolder, oName+ ".bmp") # take off .tif and replace with .bmp
    arcpy.CopyRaster_management(rasterFile,outRaster,"#","-3.402823e    +038","NONE","NONE","8_BIT_UNSIGNED","ScalePixelValue","NONE")

Copy Raster only needs you to set the file extension correctly for the file formats that it will do

3
  • I have a question about defining the in/out folders-- your code specifies the workspace as my original out folder which is empty, and 'outFolder' is not defined. If I would like to have separate in and out folders, how would I set the workspace/s?
    – AF2k15
    Jan 15, 2015 at 20:21
  • Sorry for my late reply, I've just come back from holiday. Yes, you're right, the arcpy.env.workspace is the folder (or geodatabase) that has the rasters to be processed, sorry I must have copy-pasted the wrong path as I slowly degenerated into holiday mode. Normally I would use sys.argv[n] or arcpy.GetParameterAsText(n) to set in and out and run the tool from batch file, command prompt or ArcCatalog toolbox. I'll fix up that path to avoid confusion. Jan 27, 2015 at 22:00
  • Thanks for the direction Michael, I will try post what code I've settled on as an edit to my original post.
    – AF2k15
    Jan 28, 2015 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.