PS打开图片辅助工具(PSAutoOpenPic工具)实现

查看 147|回复 10
作者:FeiyuYip   
在ps修图时候,使用ps依次选择要打开的图片,使用体验存在不顺畅感,因为有时会忘记需要打开几张图片了,基于上述原因,我尝试使用python调用ps,实现自动打开图片
1.使用photoshop_python_api
编写ps脚本一般使用JavaScript等语言,由于我不会那些,就找了一个支持python调用photoshop的库photoshop_python_api,主页见这里,操作示例见这里
首先安装这个库
[Python] 纯文本查看 复制代码pip install photoshop_python_api
由于只需要实现打开图片功能,所以打开图片的核心代码如下:
[Python] 纯文本查看 复制代码# Import local modules
from photoshop import Session
import photoshop.api as ps
# style 1
app = ps.Application()
app.load("your/psd/or/psb/file_path.psd")
# style 2
with Session("your/psd/or/psb/file_path.psd", action="open") as ps:
    ps.echo(ps.active_document.name)
style1 和style2两种任选其一即可
2.ps自动打开图片功能实现
修图时候有两个路径,一个是源图路径,ps修好后,会另存到保存路径(这一步可以用ps的动作实现,故只考虑打开的情况)
实现的思路是程序在后台定时查询两个路径的情况,通过比对筛选出存在于源图路径而不存在于保存路径(即还未修的图),放入list中,然后打开list的第1张图
当保存路径的内容有变化时,再次对比,再次打开下一张图,从而实现了自动打开待修的图片
两点说明:
1.在程序同路径新建一个“路径.txt",第一行填写源图路径,第二行填写保存路径。程序会读取这两个路径的内容
2.源图路径及保存路径相同图片的文件名必须一样,图片格式(后缀)可以不一致,即图片处理后,可以保存为其他格式
完整代码如下:
[Python] 纯文本查看 复制代码from photoshop import Session
import os
import re
import time
import copy
def make_dirs(path):
    if os.path.exists(path):
        pass
    else:
        os.makedirs(path)
def get_index_of_image_name(element):
    # 正则提取其中的数字
    try:
        # 未導入re不會報錯,注意
        num_list = re.findall(r'\d+', element)
        num = ''.join(num_list)  # 所有數字合併成一串數字,排序才準確
        if num == '':
            num = -1
    except:
        num = -1
        # num_plus = -1
    # int型排序,所以需要返回int型
    # print(num_plus)
    # return int(num_plus)
    # print(num)
    return int(num)
def handle_message(path_yuantu, path_save):
    image_yuantu_list = get_chushi_images(path_yuantu)
    image_yuantu_list.sort(key=get_index_of_image_name)
    image_yuantu_num = len(image_yuantu_list)
    # print(image_yuantu_list)
    print(f'源图路径为:{path_yuantu}')
    print(f'源图图片共有:{image_yuantu_num} 张')
    print(f'保存图片路径为:{path_save}')
    image_saved_num_temp = -1  # 初始值不能为0,因为当一张都没有拼的时候,会出问题。
    start_time = 0
    while True:
        image_saved_list = get_saved_images(path_save)
        image_saved_num = len(image_saved_list)
        # print(image_saved_num, image_saved_num_temp)
        # 当一张都没有拼的时候,两个值会同时等于0,所以上面image_saved_num_temp的初始值应为-1
        if not image_saved_num_temp == image_saved_num:
            print('*******************************')
            end_time = time.time()
            print(f'当前时间:{time.ctime()}')
            if not start_time == 0:
                time_spend = round(end_time - start_time, 2)
                print(f'处理上张图片耗时:{time_spend} s')
            start_time = time.time()
            print(f'已处理图片共有{image_saved_num}张')
            # print(image_saved_num)
            # 考虑文件名,不考虑后缀
            image_saved_list_temp = [image_saved.split('.')[0] for image_saved in image_saved_list]
            # # 找出还未处理的图片
            image_not_handle_list = [image for image in image_yuantu_list if
                                     not image.split('.')[0] in image_saved_list_temp]
            image_not_handle_list.sort(key=get_index_of_image_name)
            # 当所有图片处理完毕后,退出!
            if len(image_not_handle_list) == 0:
                break
            else:
                # # # 每次打开第一个
                print(f'接下来要打开的图片是:{image_not_handle_list[0]}')
                image_to_open_path = path_yuantu + '/' + image_not_handle_list[0]
                # 调用ps程序,打开图片
                ps_open_image(image_to_open_path)
                # 将保存图片的张数赋给临时保存
                image_saved_num_temp = image_saved_num
        time.sleep(1)
    print('*******************************')
    print('所有图片已经处理完毕,建议再检查一遍!!')
    os.system('pause')
def ps_open_image(image_path):
    # import photoshop.api as ps
    # app = ps.Application()
    # app.load(image_path)
    with Session(image_path, action="open") as ps:
        ps.echo(ps.active_document.name)
def get_chushi_images(path_yuantu):
    image_yuantu_list = os.listdir(path_yuantu)
    image_yuantu_list = [image for image in image_yuantu_list if not 'seka' in image and not '.txt' in image]
    return image_yuantu_list
def get_saved_images(path_save):
    image_saved_list = os.listdir(path_save)
    image_saved_list = [image for image in image_saved_list if not 'seka' in image and not '.txt' in image]
    # image_saved_path_list = [path_save + '/' + image for image in image_saved_list]
    return image_saved_list
def readFileText(txt_name):
    # 讀取txt文件,並保存到列表中
    imgUrlStringList = []
    with open(txt_name, 'r', encoding='UTF-8-sig') as f:
        for line in f:
            imgUrlStringList.append(line.strip('\n'))
    return imgUrlStringList
def main():
    txt_name = '路径.txt'
    path_list = readFileText(txt_name)
    path_yuantu = path_list[0]
    path_save = path_list[1]
    # print(path_yuantu)
    # print(path_save)
    make_dirs(path_save)
    # print(path_yuantu, path_save)
    handle_message(path_yuantu, path_save)
if __name__ == '__main__':
    main()
2023.1.3 更新成品链接:
链接:https://pan.baidu.com/s/1hKyUH2b4A5GnCQzIhcnBpg
提取码:qwej

路径, 图片

FeiyuYip
OP
  


8970665 发表于 2022-12-23 12:13
期待成品

链接:https://pan.baidu.com/s/1hKyUH2b4A5GnCQzIhcnBpg
提取码:qwej
--来自百度网盘超级会员V5的分享
jwzb   

学习了,很厉害啊
8970665   

期待成品
feiyu361   

感谢,学习了
cdyangjian   

学习了。。。。
lbg2222000   

感谢分享
lgjcsw   

很不错的,期待成果
cao884   

厉害了我也是期待成品
15038691248   

支持一波,加油
您需要登录后才可以回帖 登录 | 立即注册

返回顶部