如何使用 arcpy 批量导出 JPG 格式图片

1. 使用 ListFiles 函数获取需要处理的所有栅格图层的路径名(这些图层必须是 ArcGIS 支持的栅格格式,例如:JPEG、TIFF、PNG 等),然后再遍历图层列表执行操作。示例代码如下:

import arcpy
from arcpy.sa import *

# Set input file path and output folder path
in_path = "D:/path/to/input/folder"
out_folder = "D:/path/to/output/folder"

# Obtain a list of all raster files in the input folder
all_files = arcpy.ListFiles("*", "Raster", in_path)

for in_file in all_files:
    # Check whether the raster is a JPEG and is worth processing
    if not (in_file.endswith(".jpg") or in_file.endswith(".jpeg")):
        continue

    # Compose the output file name and path
    out_file = in_file[:-4] + ".jpg"
    out_path = out_folder + "/" + out_file

    # Execute the RasterToOtherFormat_conversion function to convert the input raster to JPEG format
    arcpy.RasterToOtherFormat_conversion(in_file, out_path, "JPEG")

2. 如果您还需要针对不同的图层指定不同的输出参数,可以在循环体中添加变量来保存这些自定义配置。例如,如果您有多个输入文件夹和多个输出文件夹,并且需要为每个图层指定输出分辨率和压缩质量,则可以创建一个字典,将输入/输出文件夹和输出参数保存在其中:

 

import arcpy
from arcpy.sa import *

# Define the input/output file locations and output parameters
file_config = {
    "input_folder": "D:/path/to/input/folder",
    "output_folder": "D:/path/to/output/folder",
    "out_resolution": 200,
    "out_quality": 80
}

# Obtain a list of all raster files in the input folder
all_files = arcpy.ListFiles("*", "Raster", file_config["input_folder"])

for in_file in all_files:
    # Check whether the raster is worth processing
    if not (in_file.endswith(".jpg") or in_file.endswith(".jpeg")):
        continue

    # Compose the output file name and path
    out_file = in_file[:-4] + ".jpg"
    out_path = file_config["output_folder"] + "/" + out_file

    # Execute the RasterToOtherFormat_conversion function to convert the input raster to JPEG format, using specified output parameters
    arcpy.RasterToOtherFormat_conversion(in_file, out_path, "JPEG", "", "", file_config["out_resolution"], "", "", "", "", "", "", file_config["out_quality"])

注意:在执行上述操作时,请确保对应文件的路径和格式信息是否正确,并检查计算机性能和资源使用情况等问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

认真学GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值