Convert jpg to tif - arcpy

9547
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
1 Solution

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

View solution in original post

27 Replies
LisaTurner
Occasional Contributor II

Have you seen this ArcGIS Desktop ? I'm not a developer, so I probably can't help with code questions, but I did notice a sample code in here that is similar to yours that I thought might help.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

Hi Lisa, yes i based my code from there- thanks

0 Kudos
XanderBakker
Esri Esteemed Contributor

The list of jpgs should be a list (string) separated by semicolons. Use arcpy.ListRasters to obtain the list of input jpgs and format the list into a string using the semicolon as separator.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

Hi Xander, i try this:

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

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

but get en error:

>>> 
BSM-1534.jpg


Traceback (most recent call last):
  File "C:\Users\htr\Desktop\python2.py", line 11, in <module>
    arcpy.RasterToOtherFormat_conversion("*.jpg","OtherFormat","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: Input Rasters: Dataset *.jpg does not exist or is not supported
Failed to execute (RasterToOtherFormat).

in the work space i do have jpg rasters

0 Kudos
IanMurray
Frequent Contributor

you need to replace

arcpy.RasterToOtherFormat_conversion("*.jpg","OtherFormat","TIFF")

with

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

List Rasters returns a list of raster string paths, so when you loop through them, raster represents the file path for the raster you want converted.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

what the meaning of the " * " ?

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

when i add the tif to mxd i get the raster and black area a round:

2015-03-30_192526.jpg

0 Kudos
DarrenWiens2
MVP Honored Contributor

Have you set the NoData value for display?

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

how can i know what to set?

0 Kudos