0

I have 120 rasters( jpg format ) and i want to convert them to tiff format to folder called "OtherFormat" in env.workspace with arcpy. In my code i try to convert all jpg file I read http://resources.arcgis.com/en/help/main/10.2/index.html#//001200000032000000 and understand that i can do it if i will write the exact name of the raster. But i have 120 rasters so i tried to use asterisk:

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project\out"
arcpy.RasterToOtherFormat_conversion("*.jpg","OtherFormat","TIFF") 
print 'converted'

but get en error:

ExecuteError: Failed to execute. Parameters are not valid.  
ERROR 000732: Input Rasters: Dataset *.jpg does not exist or is not supported Failed to execute (RasterToOtherFormat). 
3

You need to make a list of the jpeg's and the n loop trough them. You can use the glob module for that:

import arcpy, os , glob

outworkspace = r"C:\Project\out\OtherFormat"
listOfJPG = glob.glob( r"C:\Project\out\*.jpg")

arcpy.RasterToOtherFormat_conversion(listOfJPG , outworkspace ,"TIFF") 

print 'converted'
  • warrieka,i get en error: Traceback (most recent call last): File "C:\Users\yaron.KAYAMOT\Desktop\stack 1.py", line 10, in <module> arcpy.RasterToOtherFormat_conversion(jpg, name,"TIFF") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\conversion.py", line 2753, in RasterToOtherFormat raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Output Workspace: Dataset 2015-03-25_084900.jpg.tif does not exist or is not supported Failed to execute (RasterToOtherFormat). – newGIS Mar 31 '15 at 7:16
  • RasterToOtherFormat_conversion takes a list as input and a workspace as output. I mistook it for Raster_copy. I edited my solution. – warrieka Mar 31 '15 at 8:26
  • arcpy.ListRasters("*","JPG") is possibly better in this case, it does not require the user to download glob. First set arcpy.env.workspace to the folder the rasters are in and then returns a list of all the JPEG raster files. – Michael Stimson Jun 26 '15 at 3:56
0

You should iterate through each file in your directory and call the function on each filename:

import os
in_direc  = 'C:\raster_loc'
out_direc = 'C:\converted'
for file in os.listdir(in_direc):
    #check to make sure it's a jpg
    if 'jpg' in file:
        #you need to change the parameters of this function call accordingly
        arcpy.RasterToOtherFormat_conversion(file,"OtherFormat","TIFF") 
  • 1
    or os.listdir works to :) – warrieka Mar 30 '15 at 15:53
  • camdenl,nothing converted with your code and also no error happened. i replace (file,"OtherFormat) with arcpy.RasterToOtherFormat_conversion(in_direc,out_direc,"TIFF") – newGIS Mar 31 '15 at 7:24
  • Perhaps reading the method documentation will help next time ;) – camdenl Mar 31 '15 at 15:05

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.