Select to view content in your preferred language

Convert jpg to tif - arcpy

10174
27
Jump to solution
03-30-2015 07:39 AM
Yaron_YosefCohen
Occasional Contributor II

Hi

I have several rasters( jpg files ) and i want convert them to tif files to file " OtherFormat" in env.workspace with arcpy. In my code i try to convert all jpg file:

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).

any help would be great

Tags (2)
0 Kudos
27 Replies
IanMurray
Frequent Contributor

in tools like ListRasters the "*" acts as a wildcard that will take any string name.  So if you have  "DEM*"  it will return any jpegs that begin with "DEM" and have any characters following it.  This does not work with arcpy.RasterToOtherFormat_conversion as an input, since it is checking for a raster at a specific file path, not attempting to find multiple rasters as the ListRasters does.  ListRasters is intended to get a list of all the rasters of the name and type you indicated, which you can then use as the input for other geoprocessing tools.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

Ian,

when i run the code i get:

>>> 
2015-03-25_084900.jpg
converted
>>> 

but when i open r"C:\Project\out\OtherFormat" there no tiff raster

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

Ian,

when i try to convert tiff to jpg with:

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

env.workspace = r"C:\Project\out\OtherFormat" 
rasters = arcpy.ListRasters("*", "TIFF") 
for raster in rasters: 
    print(raster) 
    arcpy.RasterToOtherFormat_conversion(rasters ,"OtherFormat","JPEG") 
    print 'converted'

the code just run without any messages and the tif raster does not converted

0 Kudos
IanMurray
Frequent Contributor

almost there.

in the arcpy.RasterToOtherFormat_conversion, you are passing along rasters, which is your list of rasters instead of the individual rasters which would just be your variable, raster.

Not this:

arcpy.RasterToOtherFormat_conversion(rasters ,"OtherFormat","JPEG")

But this:

arcpy.RasterToOtherFormat_conversion(raster ,"OtherFormat","JPEG")

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

the result is:

>>> ================================ RESTART ================================
>>> 
>>> 

and there no converted rasters in OtherFormat folder. i tried also to convert bmp and png format to jpg- with no success .

0 Kudos
LisaTurner
Occasional Contributor II

I'm not sure if you need an app within Arc to do this or not, but I do know that I have successfully converted jpegs to tiffs in a free application called QGIS. It lets you keep the same projection, and isn't too difficult to use. No coding required. Granted, I'm fairly certain the programmers in here likely could keep working with you until the code works.

0 Kudos
IanMurray
Frequent Contributor

Lisa, programming isn't required to do this for a single  tif, but it sounds like she wants to convert an entire folders worth of tifs to jpegs.  It fine to go and do manually if you have just a few, but it sound like they could have many tifs to be converted, so a script to do it all could save lots of time. 

0 Kudos
LisaTurner
Occasional Contributor II

Hey Ian,

I do batches of a few hundred at a time on QGIS all the time. The only manual step would be the input test of one or two to make sure the output is how someone needs it to be. It's all good though as I am learning so much from watching you all adjust the script.

0 Kudos
IanMurray
Frequent Contributor

I'm still just a student and haven't been able to look into Open Source GIS a whole lot, though it sounds like it would be fairly easy to do in QGIS, more so then running a tool like that in batch mode in ArcGIS.  Python just helps augment ArcGIS geoprocessing tools to make them more effective. 

XanderBakker
Esri Esteemed Contributor

There are several errors in your code. Have a look at the code below:

import arcpy

in_folder = r"C:\Forum\TIFF2JPG\tiff"
out_folder = r"C:\Forum\TIFF2JPG\jpg"

arcpy.env.workspace = in_folder
rasters = arcpy.ListRasters("*", "TIF")
print rasters
for raster in rasters:
    print raster
    # RasterToOtherFormat_conversion (Input_Rasters, Output_Workspace, {Raster_Format})
    arcpy.RasterToOtherFormat_conversion(raster, out_folder, "JPEG")

Changes to the code:

  • I left out all the imports that you were not using, just left the arcpy
  • I created to variables to store valid input and output folders (can be the same)
  • On line 7 you used "TIFF" as format, if you look at the help you will see that you should use "TIF"
  • On line 12 I use the out_folder variable for the output workspace. You specified "OtherFormat" which isn't a valid workspace

The result that is printed is

[u'image00.tif', u'image01.tif', u'image02.tif', u'image03.tif']
image00.tif
image01.tif
image02.tif
image03.tif

The out_folder contains the 4 jpg's names image00.jpg to image03.jpg including wordfiles.

No "black areas" are invented in the output. However if your input has a larger area than your data extent, this will be exported too. Also when run within the Python window of ArcMap, extent environment settings will influence the result. The black area is how in TIFF imagery the nodata is represented by default.

To convert JPG to TIFF just invert the TIF and JPEG settings (and point to the correct folders).