PDF转图片?ArcGIS Python来实现!(含源代码,工具下载)
The following article is from gisoracle Author 闫磊ArcGIS
网页上很多只能上传图片,如微信公众号,不能直接是PDF,很多时候我们得到是PDF,怎么办,使用ArcGIS的两个工具
1.PDF转TIF
代码类似:
arcpy.PDFToTIFF_conversion(in_pdf_file="E:/py/pdf/耿老师day1学习笔记-tina.pdf", out_tiff_file="C:/Users/DELL/Documents/ArcGIS/耿老师day1学习笔记tina1.tif", pdf_password="*****", pdf_page_number="1", pdf_map="", clip_option="NO_CLIP", resolution="250", color_mode="RGB_TRUE_COLOR", tiff_compression="LZW", geotiff_tags="NO_GEOTIFF_TAGS")
2.TIF转其他格式图片,TIF太大
代码类似:
arcpy.CopyRaster_management(in_raster="耿老师day1学习笔记tina1.tif", out_rasterdataset="E:/py/dd.png", config_keyword="", background_value="", nodata_value="256", onebit_to_eightbit="NONE", colormap_to_RGB="NONE", pixel_type="", scale_pixel_value="NONE", RGB_to_Colormap="NONE", format="PNG", transform="NONE")
总的代码:
# -*- coding: utf-8 -*-import arcpy
import sys
import os
def initProgress(hint,num):
arcpy.SetProgressor("step", u"正在"+hint,0,num,1)
def step():
arcpy.SetProgressorLabel(u"正在进行....")
arcpy.SetProgressorPosition()
def freeProgress():
arcpy.ResetProgressor()
def pdftojpg(inPDF,outTIFF):
# Create PDFDocument Object from inPDF
pdf = arcpy.mapping.PDFDocumentOpen(inPDF)
# Loop through each page in the PDF and create a name based on the page number
initProgress("exp png",pdf.pageCount)
for page in range(1, pdf.pageCount+1):
step()
tifname = "yl"+str(page) + ".tif"
outTIFFpath = os.path.join(outTIFF, tifname)
# Export each page to TIFF using 96 DPI, CMYK color mode, and JPEG compression
arcpy.PDFToTIFF_conversion(inPDF, outTIFFpath, '#', str(page), '#', '#', 96, 'CMYK_TRUE_COLOR', 'JPEG')
jpgname =str(page) + ".png"
outjpgpath = os.path.join(outTIFF, jpgname)
arcpy.AddMessage("outjpgpath="+outjpgpath)
#arcpy.AddMessage("outTIFFpath=" + outTIFFpath)
arcpy.CopyRaster_management(in_raster=outTIFFpath,
out_rasterdataset=outjpgpath,
config_keyword="", background_value="", nodata_value="256",
onebit_to_eightbit="NONE",
colormap_to_RGB="NONE", pixel_type="", scale_pixel_value="NONE",
RGB_to_Colormap="NONE", format="PNG", transform="NONE")
# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "1.tif"
arcpy.Delete_management(in_data=outTIFFpath, data_type="")
# Build pyramids and calculate statistics on each output TIFF
#arcpy.BuildPyramidsandStatistics_management(outTIFF)
#print "Exported " + outTIFFpath
freeProgress()
inPDF = arcpy.GetParameterAsText(0) #原始的PDDF
outPath = arcpy.GetParameterAsText(1) #输出文件夹
if not os.path.exists(outPath):
os.makedirs(outPath)
pdftojpg(inPDF,outPath)
运行界面:
治愈编程,会手工操作,就会编程,操作视频
工具下载:ArcGIS10.1以上使用
链接:https://pan.baidu.com/s/1DFfV74qzqeEKcjTk04U2qQ
提取码:ylok
- END -