-1

ArcGIS 10.2 and I use ArcPy and I have create a ArcPy script. I want my final raster from script to create two raster datasets with different extensions, a TIFF extension and a PNG extension.

Here is the code:

minvalue = arcpy.GetRasterProperties_management(final, "MINIMUM")
minvalue2 = (minvalue.getOutput(0))
maxvalue = arcpy.GetRasterProperties_management(final, "MAXIMUM")
maxvalue2 = (maxvalue.getOutput(0))
# Process: Reclassify (6)
arcpy.gp.Reclassify_sa(final, "VALUE","{0} 6 1;7 32 2;33 162 3;163 512 4;513 1250 5;1250 {1} 6".format(minvalue2, maxvalue2), final1,"DATA")
try:
    arcpy.RasterToOtherFormat_conversion (final1,"OtherFormat", {"PNG"})
except:
    print "Raster To Other Format exsample failed."
    print arcpy.GetMessages()

message:

Raster To Other Format exsample failed.
Executing: Reclassify in_memory/final VALUE "0 6 1;7 32 2;33 162 3;163 512 4;513 1250 5;1250 39370526556160 6" C:\Users\username\Desktop\myimage.tif DATA
Start Time: Wed Feb 28 23:10:44 2018
Succeeded at Wed Feb 28 23:10:46 2018 (Elapsed Time: 2,03 seconds)

Here is the second test for the conversion:

from PIL import Image

im = Image.open('C:/Users/username/Desktop/test.tif')
new_im = im.convert('RGBA')
new_im.save('test.jpg', 'JPEG', quality=100)

error:

Traceback (most recent call last):
  File "C:/Users/username/test/newt.py", line 5, in <module>
    new_im = im.convert('RGBA')
  File "C:\Python27\Lib\site-packages\PIL\Image.py", line 857, in convert
    self.load()
  File "C:\Python27\Lib\site-packages\PIL\TiffImagePlugin.py", line 1011, in load
    return super(TiffImageFile, self).load()
  File "C:\Python27\Lib\site-packages\PIL\ImageFile.py", line 204, in load
    decoder.setimage(self.im, extents)
ValueError: tile cannot extend outside image

Third test:

try:
    arcpy.RasterToOtherFormat_conversion(final1,"OtherFormat","PNG")
except:
    print "Raster To Other Format exsample failed."
    print arcpy.GetMessages()

message :

Raster To Other Format exsample failed.
Executing: RasterToOtherFormat C:/Users/username/myimage.tif OtherFormat PNG
Start Time: Wed Feb 28 23:24:34 2018
Failed to execute. Parameters are not valid.
ERROR 000732: Output Workspace: Dataset OtherFormat does not exist or is not supported
Failed to execute (RasterToOtherFormat).
Failed at Wed Feb 28 23:24:34 2018 (Elapsed Time: 0,00 seconds)

final test:

arcpy.gp.Reclassify_sa(final, "VALUE","{0} 6 1;7 32 2;33 162 3;163 512 4;513 1250 5;1250 {1} 6".format(minvalue2, maxvalue2), final1,"DATA")
try:
    out_rasterdataset='test.png'
    arcpy.CopyRaster_management(final1, out_rasterdataset)
except:
    print "Raster To Other Format exsample failed."
    print arcpy.GetMessages()

message:

Raster To Other Format exsample failed.
Executing: CopyRaster C:/Users/username/myimage.tif test.tif # # 1 NONE NONE # NONE NONE
Start Time: Wed Feb 28 23:35:49 2018
ERROR 999999: Error executing function.
Failed to execute (CopyRaster).
Failed at Wed Feb 28 23:35:49 2018 (Elapsed Time: 0,03 seconds)

Any idea where I am going wrong?

  • arcpy.RasterToOtherFormat_conversion (final1,"OtherFormat", {"PNG"})? isn't right. The args are Input_Rasters, Output_Workspace, {Raster_Format}, Final1 is right but the 2nd argument is the folder you want the raster to end up in and the 3rd argument isn't a dictionary so it's just "PNG", the parenthesis in the docs is just telling you that the argument is optional not to put parenthesis around the argument. – Michael Stimson Feb 28 '18 at 22:48
3

From ArcGIS 10 there are new objects for rasters, you are using the old (9.3) methods but you will find better functionality with the new objects.

Most of your trouble comes from not specifying exactly where to put your raster and from misunderstanding the help.. when the help puts parenthesis around a parameter it means optional not put a parenthesis around your parameter, that would make {"PNG"} a dictionary with a key but no value..

Try this:

import os
OutputWorkspace = r'c:\some\path' # where you want to put the files output
OutputTIFF      = "Output.tif"    # the name of the TIFF file
OutputPNG       = "Output.png"    # the name of the PNG file

minvalue = arcpy.GetRasterProperties_management(final, "MINIMUM")
minvalue2 = float(minvalue.getOutput(0)) # make sure minimum is numeric
maxvalue = arcpy.GetRasterProperties_management(final, "MAXIMUM")
maxvalue2 = float(maxvalue.getOutput(0)) # make sure maximum is numeric
# Process: Reclassify (6)
# make a RemapRange 
Remap = arcpy.sa.RemapRange([[minvalue2,6,1],[7,32,2],[33,162,3],[163,512,4],[513,1250,5],[1250,maxvalue2,6]])
# reclassify the raster to a new raster 
ReclassRaster = arcpy.sa.Reclassify(final,"VALUE",Remap,"DATA")
# ReclassRaser is now a raster object 
ReclassRaster.save(os.path.join(OutputWorkspace,OutputTIFF))
ReclassRaster.save(os.path.join(OutputWorkspace,OutputPNG))

The newer help for Reclassify uses a RemapRange object which is a list of lists rather than a semicolon and space delimited string, this produces a raster that has a .save method but be sure to be specific about your output location; if not specified your current workspace could be None which is why you're having trouble saving the file, either set arcpy.env.workspace or specify the full path in your filesystem to store the output raster.

  • i try to test your code but i get some error again,only with GDAL transate i can to convert that images,but i wanto to do using arcpy – hellas Mar 3 '18 at 20:56
  • 1
    What error? What does you current code look like? Can you edit your question to include the code as it is currently and the text of the error message. I can see a syntax error which I will fix up now. – Michael Stimson Mar 4 '18 at 22:00

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.