Reputation: 19
I'm creating a program in python using pyautogui that will automatize my process of creating PNGs so I can later convert them into ICO files. The goal is to dump the files in a folder and after converting them, saving them in another folder. This process is not complete yet but it is not important for my question. I'll pass my code. It has notations so if you know photoshop you'll understand what I'm doing. After the code I'll give more info and say what's wrong
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","shift","shift","shift","shift","shift","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","shift","<") #same as above but horizontaly
pg.hotkey("ctrl","shift","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
This, as it is, should be able to at least create one of those squared PNGs, however, when minimizing my compiler and opening my photoshop, nothing happens. I know the program works because when I don't close my compiler during those initial 10 seconds, my mouse starts to move and new files are created and things written (because my compiler also uses the same shortcuts as PS). Why doesn't it work with the when PS is opened? If someone knows I'd also like to know why the function resolution doesn't support PNG files In case what I said is confusing I uploaded here 3 images that have my goals and a mid-process screenshot. I know this is a little bit too "personal" of a program, for lack of a better word, and there for it can be confusing so if someone has any doubt or if I made any mistake please let me know
Photos: https://imgur.com/a/6DWXcYu
Upvotes: 1
Views: 172
Reputation: 11
To use pyautogui in any program, you have to run your IDE (IDLE) as Admin. Search for IDLE, click with the right mouse button, then click in "run as admin" open the code file you want to run, then it works.
Upvotes: 1