微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Pyautogui 的 Python 程序在 Photoshop 上不起作用

如何解决使用 Pyautogui 的 Python 程序在 Photoshop 上不起作用

我正在使用 pyautogui 在 python 中创建一个程序,该程序将自动化我创建 PNG 的过程,以便我以后可以将它们转换为 ICO 文件。目标是将文件转储到一个文件夹中,转换后将它们保存在另一个文件夹中。这个过程还没有完成,但对我的问题来说并不重要。我会通过我的代码。它有符号,所以如果你知道 photoshop 你就会明白我在做什么。在代码之后,我会提供更多信息并说明有什么问题

import pyautogui as pg
from time import sleep
from PIL import Image
from PIL.ExifTags import TAGS



def resolution(image):                                                          #this will select an image as an argument using its directory and find the dimensions of it
    imagem = Image.open(image)
    exif ={TAGS[k]:v for k,v in imagem._getexif().items() if k in TAGS}
    return (exif["ExifImageWidth"],exif["ExifImageHeight"])

i = """C:\\Users\PriestMarmore\Desktop\Photoshop\Originais\Azeite.jpg"""        #this is the image introduced manually. later on it will be through a function
sleep(10)                                                                       #time to go to photoshop and make sure everything is ok

pg.click(2000,300)                                                              #random click on the middle of photoshop to make sure PS is the main app being used

#Archive -> New
w = resolution(i)[0]                                                            #attributes to w the value of the image width
h = resolution(i)[1]                                                            #attributes to h the value of the image height

if w > h:
    size = w
else:                                                                           #this 4 lines will select the highest value between w and h to later create a transparent squared layer with the highest value dimension
    size = h
    
pg.hotkey("ctrl","n")                                                           #creates new file/layer
pg.typewrite(["shift","shift"])                                                 #goes to the width input area
pg.typewrite(str(size))                                                         #writes the dimensions of the squared layer,determined above
pg.typewrite(["shift","shift"])                                                 #goes to the height input area
pg.typewrite(str(size))                                                         #same as above
pg.typewrite(["shift","shift","enter"])#clicks on OK.

#Archive -> Open
pg.hotkey("ctrl","o")                                                           #opens a file
pg.doubleClick(547,188)                                                         #select the first image of the selected folder. the folder is always on the same position and on the same directory

#copy Layer
pg.rightClick(2388,855)                                                         #opens menu of layer
pg.click(2388,169)                                                              #selects to duplicate layer
pg.typewrite(["tab","up","enter"])                                              #opens menu of directory and then selects the first layer on photoshop,which is the squared transparent layer. finally confirms it
pg.hotkey("ctrl","w")                                                           #closes the selected image,only lefting the transparesnt squared layer with the image we selected as a second layer

#Select Layers
pg.move(50,92)                                                                  #clicks on the top-left part of PS
pg.drag(2179,1149)                                                              #Corresponds to a vector where it will always fully select the transparent squared layer
pg.hotkey("ctrl","<")                                                           #this is a shortcut that will center the image on the vertical
pg.hotkey("ctrl","<")                                                   #same as above but horizontaly
pg.hotkey("ctrl","s")                                                   #opens "save" menu
pg.write("Azeite")                                                              #This will have the name of the original file but I haven't done that yet so I putted a manual one
pg.click(690,888)                                                               #Clicks on the format list
pg.click(690,1116)                                                              #Selects PNG
pg.typewrite(["enter","enter"])                                                 #Double enter in order to confirm
sleep(30)                                                                       #Big images take around 20 seconds to be saved so this will create more than that time since I can create a way to see when it is finished
pg.hotkey("ctrl","w")                                                           #Closes the file
pg.typewrite(["right"])                                                         #A pop-up appeared asking if I wanna save the changes. This selects no
pg.typewrite(["enter"])                                                         #Confirms the prevIoUs step and closes the file/layer,leaving nothing open and being ready for other files

事实上,这至少应该能够创建这些平方 PNG 中的一个,但是,当最小化我的编译器并打开我的 photoshop 时,什么也没有发生。我知道该程序有效,因为当我在最初的 10 秒内没有关闭编译器时,我的鼠标开始移动并创建新文件并写入内容(因为我的编译器也使用与 PS 相同的快捷方式)。为什么它在打开PS时不起作用? 如果有人知道我也想知道为什么函数分辨率不支持PNG文件 以防我所说的令人困惑,我在这里上传了 3 张有我的目标的图片一个中间过程截图。我知道这对程序来说有点过于“个人化”,因为没有更好的词,而且它可能会令人困惑,所以如果有人有任何疑问或我犯了任何错误,请告诉我

照片:https://imgur.com/a/6DWXcYu

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。