0
点赞
收藏
分享

微信扫一扫

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)

时光已翩然轻擦 2022-11-14 阅读 173


文章目录

  • ​​1、简介​​
  • ​​2、Photoshop批处理​​
  • ​​2.1 创建动作​​
  • ​​2.2 脚本​​
  • ​​2.3 图像处理器​​
  • ​​2.4 批处理​​
  • ​​3、Python批处理​​
  • ​​3.1 comtypes + Photoshop​​
  • ​​3.2 win32com + Photoshop​​
  • ​​3.3 photoshop_python_api​​
  • ​​3.4 PIL​​
  • ​​结语​​

1、简介

Photoshop,简称“PS”,是Adobe公司推出的一款图像处理软件。

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_gis

Photoshop的专长在于图像处理,而不是图形创作。图像处理是对已有的位图图像进行编辑加工处理以及运用一些特殊效果,其重点在于对图像的处理加工;图形创作软件是按照自己的构思创意,使用矢量图形等来设计图形。

2、Photoshop批处理

​​https://helpx.adobe.com/cn/photoshop/using/processing-batch-files.html​​

2.1 创建动作

对于经常执行的任务,您可以轻松地记录若干动作以帮助您更加高效地工作。
记录动作时请记住以下原则:

  • 可以在动作中记录大多数(而非所有)命令。
  • 您可以记录用“选框”、“移动”、“多边形”、“套索”、“魔棒”、“裁剪”、“切片”、“魔术橡皮擦”、“渐变”、“油漆桶”、“文字”、“形状”、“注释”、“吸管”和“颜色取样器”工具执行的操作,也可以记录在“历史记录”、“色板”、“颜色”、“路径”、“通道”、“图层”、“样式”和“动作”面板中执行的操作。
  • 结果取决于文件和程序设置变量,如现用图层和前景色。例如,3 像素高斯模糊在 72 ppi 文件上创建的效果与在 144 ppi 文件上创建的效果不同。“色彩平衡”也不适用于灰度文件。
  • 如果记录的动作包括在对话框和面板中指定设置,则动作将反映在记录时有效的设置。如果在记录动作的同时更改对话框或面板中的设置,则会记录更改的值。
  • 打开文件。
  • 在“动作”面板中,单击“创建新动作”按钮 ,或从“动作”面板菜单中选择“新建动作”。
  • 输入一个动作名称,选择一个动作集,然后设置附加选项:
  • 单击“开始记录”。“动作”面板中的“开始记录”按钮 变为红色。
  • 执行要记录的操作和命令。
    并不是动作中的所有任务都可以直接记录;不过,可以用“动作”面板菜单中的命令插入大多数无法记录的任务。
  • 要停止记录,请单击“停止播放/记录”按钮,或从“动作”面板菜单中选择“停止记录”,或按 Esc 键。

2.2 脚本

Photoshop 通过脚本支持外部自动化。在 Windows 中,可以使用支持 COM 自动化的脚本语言,例如 VB Script。在 macOS 中,可以使用允许发送 Apple 事件的语言,例如 AppleScript。这些语言不是跨平台的,但可以控制多个应用程序,例如 Adobe Photoshop、Adobe Illustrator 和 Microsoft Office。在 macOS 中,也可以使用 Apple 的 Photoshop Actions for Automator 来控制 Photoshop 中的任务。

也可以在这两种平台上使用 Javascript。您可以利用 JavaScript 支持编写可以在 Windows 或 macOS 上运行的 Photoshop 脚本。

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_map_02

2.3 图像处理器

图像处理器可以转换和处理多个文件。与“批处理”命令不同,您不必先创建动作,就可以使用图像处理器来处理文件。

您可以在图像处理器中执行下列任何操作:

  • 将一组文件转换为 JPEG、PSD 或 TIFF 格式之一,或者将文件同时转换为所有三种格式。
  • 使用相同选项来处理一组相机原始数据文件。
  • 调整图像大小,使其适应指定的像素大小。
  • 嵌入颜色配置文件或将一组文件转换为 sRGB,然后将它们存储为用于 Web 的 JPEG 图像。
  • 在转换后的图像中包括版权元数据。

请执行下列任一操作:

  • 选取“文件”>“脚本”>“图像处理器”(Photoshop)。
  • 选取“工具”>“Photoshop”>“图像处理器”(Bridge)。

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_python_03

2.4 批处理

“批处理”命令可以对一个文件夹中的文件运行动作。如果您有带文档输入器的数码相机或扫描仪,也可以用单个动作导入和处理多个图像。扫描仪或数码相机可能需要支持动作的取入增效工具模块。

执行下列操作之一:

  • 选取“文件”>“自动”>“批处理”(Photoshop)。
  • 选取“工具”>“Photoshop”>“批处理”(Bridge)。

(1)地图数据文件准备如下:

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_map_04

(2)文件扩展名批量修改。
由于之前下载的时候,图片扩展名写错写成了png,实际是jpg。

import os
import shutil

find_basepath = r'C:\Users\tomcat\Desktop\map_data'
new_basepath = r'C:\Users\tomcat\Desktop\map_data2'
new_ftype = ".jpg"

### 删除指定文件内的所有空文件夹
def RemoveBlankDir(find_dir):
all_file_list = os.listdir(find_dir)

if len(all_file_list) == 0:
shutil.rmtree(find_dir)
else:
for file_name in all_file_list:
current_dir = os.path.join(find_dir, file_name)

if os.path.isdir(current_dir):
# print(find_dir + "》》》" + current_dir)
RemoveBlankDir(current_dir)

### 重命名文件扩展名
def ListAndRenameAllFiles(find_dir):
all_file_list = os.listdir(find_dir)
new_path = find_dir.replace(find_basepath, new_basepath)

if(os.path.exists(new_path)):
shutil.rmtree(new_path)
os.mkdir(new_path)

for file_name in all_file_list:
current_dir = os.path.join(find_dir, file_name)

if os.path.isdir(current_dir):
# print(find_dir + "》》》" + current_dir)
ListAndRenameAllFiles(current_dir)
else:
fname = os.path.splitext(file_name)[0]
ftype = os.path.splitext(file_name)[1]
# print(current_dir, fname, ftype)

new_filepath = os.path.join(find_dir, fname + new_ftype)
new_filepath = new_filepath.replace(find_basepath, new_basepath)
shutil.copyfile(current_dir, new_filepath)
print(new_filepath)

RemoveBlankDir(find_basepath)
ListAndRenameAllFiles(find_basepath)
print("ListAndCopyAllFiles ok.")

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_gis_05

(4)新建动作

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_python_06


(5)录制动作

创建好后动作录制就开始了,看左下角有一个红点,说明正在录制,你的每一步操作都会被记录。

完成以后执行【停止录制】。等所有都操作完成后关闭图片,然后停止录制,一个完整的动作就建好了。

提示:在保存添加好动作命令时,经常会遇到存储动作的选项是灰色不可用状态(点击动作面板上方的三条杠的按钮,弹出发现存储动作是灰色的),这是因为要添加组才能存储。

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_gis_07


【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_pil_08

(6)执行批处理操作

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_photoshop_09


处理前:

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_map_10

处理后:

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_gis_11

3、Python批处理

  • Photoshop也有它的对象模型:

3.1 comtypes + Photoshop

  • 安装库:

pip install comtypes

  • 测试代码如下:

from comtypes.client import CreateObject

# 程序会连接到一个正在活动的PhotoShop实例
# Tips:如果没有正在活动的PhotoShop,那程序会自动启动一个PhotoShop
ps_app = CreateObject("Photoshop.Application", dynamic=True)

# 新建一个文档
"""
先设置好默认单位
创建一个1024*1024*72像素的new_test文档
"""
ps_app.Preferences.RulerUnits = 1
doc = ps_app.Documents.Add(1024, 1024, 72, "new_test", 2, 1, 1)

# 新建图层
art_layer = doc.ArtLayers.Add()
art_layer.name="first_art_layer"

layer_set = doc.LayerSets.Add()
layer_set.name = "first_layer_set"

art_layer_2 = layer_set.ArtLayers.Add()
art_layer_2.name = "second_art_layer"

# 关闭文档
doc.Close()

# 关闭PhotoShop
ps_app.quit()

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_python_12

#设置背景色
background_color = CreateObject('Photoshop.SolidColor')
background_color.rgb.red = 128
background_color.rgb.green = 128
background_color.rgb.blue = 255

#填充图层
new_doc.selection.Fill(background_color)

#设置保存PSD选项
psd_options = CreateObject('Photoshop.PhotoshopSaveOptions')
psd_options.annotations = False
psd_options.alphaChannels = True
psd_options.layers = True
psd_options.spotColors = True
psd_options.embedColorProfile = True

#保存PSD
new_doc.SaveAs(r"本地路径.psd", psd_options,True)
#保存TGA
new_doc.SaveAs(r"本地路径.tga", tga_options,True)

3.2 win32com + Photoshop

  • 测试代码如下:

import os

from win32com.client import Dispatch, GetActiveObject, GetObject

def getFileList(dir, Filelist, ext=None):
"""
获取文件夹及其子文件夹中文件列表
输入 dir:文件夹根目录
输入 ext: 扩展名
返回: 文件路径列表
"""
newDir = dir
if os.path.isfile(dir):
if ext is None:
Filelist.append(dir)
else:
if ext in dir[-3:]:
Filelist.append(dir)

elif os.path.isdir(dir):
for s in os.listdir(dir):
newDir = os.path.join(dir, s)
getFileList(newDir, Filelist, ext)
return Filelist

# 检索文件
org_img_folder = r'C:\Users\tomcat\Desktop\map_data2'
imglist = getFileList(org_img_folder, [], 'jpg')
print('Found: ' + str(len(imglist)) + ' 个jpg文件\n')

if len(imglist):
try:
app = GetActiveObject("Photoshop.Application")
except:
app = Dispatch("Photoshop.Application")

for imgpath in imglist:
imgname = os.path.splitext(os.path.basename(imgpath))[0]
print(imgpath)

docRef = app.Open(imgpath)
app.DoAction('地图修改动作 1','组 1')
docRef.save()
docRef.Close()

3.3 photoshop_python_api

  • 安装库如下:

pip install photoshop_python_api

  • 测试代码如下:

import photoshop.api as ps

app = ps.Application()
doc = app.open(r"C:\Users\tomcat\Desktop\test_map\map\107750.png")
# doc = app.documents.add()

new_layer = doc.artLayers.add()

text_color = ps.SolidColor()
text_color.rgb.red = 255
text_color.rgb.green = 0
text_color.rgb.blue = 255

new_text_layer = new_layer
new_text_layer.kind = ps.LayerKind.TextLayer
new_text_layer.textItem.contents = '爱看书的小沐!'
new_text_layer.textItem.position = [10, 50]
new_text_layer.textItem.size = 25
new_text_layer.textItem.color = text_color

options = ps.JPEGSaveOptions(quality=5)

# # save to jpg
jpg = 'd:/hello_world.jpg'
doc.saveAs(jpg, options, asCopy=True)
app.doJavaScript(f'alert("save to jpg: {jpg}")')

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_python_13

3.4 PIL

from PIL import Image
from PIL import ImageFilter ## 调取ImageFilter

new_path = r'C:\Users\tomcat\Desktop\map_data(new)'
img = Image.open(r"C:\Users\tomcat\Desktop\map_data2\5\17\9.jpg")
blu = img.filter(ImageFilter.BLUR) ##均值滤波
con = img.filter(ImageFilter.CONTOUR) ##找轮廓
edge = img.filter(ImageFilter.FIND_EDGES) ##边缘检测
edge_en = img.filter(ImageFilter.EDGE_ENHANCE) ##边缘检测

blu.save(new_path + '/k.jpg')
con.save(new_path + '/k2.jpg')
edge.save(new_path + '/k3.jpg')
edge_en.save(new_path + '/k4.jpg')

  • 处理前:
  • 【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_map_14


  • 处理后:
  • 【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_python_15


from PIL import Image

def blend_two_images():
img1 = Image.open( r"C:\Users\tomcat\Desktop\test_map\map\107750.png")
img1 = img1.convert('RGB')

img2 = Image.open( r"C:\Users\tomcat\Desktop\test_map\text\107750.png")
img2 = img2.convert('RGB')

img = Image.blend(img1, img2, 0.5)
img.show()
img.save( r"result.png")

blend_two_images()

  • 处理前(底图层和文字层):


【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_photoshop_16

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_pil_17


  • 处理后(合并层):
  • 【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_gis_18


结语

​如果您觉得该方法或代码有一点点用处,可以给作者点个赞​​╮( ̄▽ ̄)╭

​如果您感觉方法或代码不咋地​​//(ㄒoㄒ)//​​,就在评论处留言,作者继续改进;​​o_O???

​如果您需要相关功能的代码定制化开发,可以留言私信作者;​​(✿◡‿◡)

​感谢各位大佬童鞋们的支持!​​( ´ ▽´ )ノ ( ´ ▽´)っ!!!

【GIS开发】Photoshop批处理GIS地图离线瓦片(Python)_map_19




举报

相关推荐

0 条评论