Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My code gives the error "Please check if you have Photoshop installed correctly." #161

Closed
emogothyy opened this issue Jul 3, 2022 · 5 comments

Comments

@emogothyy
Copy link

Hello,
I'm trying to collect information from a user using telegram chat (pyTelegramBotAPI) so I can transfer this to photoshop inside the function in the future, but when I use the bot, the code gives the error "Please check if you have Photoshop installed correctly." Although if you run the same, but not through the bot everything will run and work fine.

I tried - get a newer version of photoshop (CC2018 -> 2020), and also specify the path in the Windows environment variables. If you open photoshop using pyWin32 it works fine, but it's very unprofitable for me, I was going to use only pythonPhotoshopAPI.

Here's the function

def test_edit_text(info_from):
try:
    psApp = ps.Application()
    psApp.Open(r"mypath\first.psd")
    doc = psApp.Application.ActiveDocument
    print(info_from['test'])


    text_from_user = info_from['test'] 
    layer1init = doc.ArtLayers["layer1"]
    text_new_layer1 = layer1init.TextItem
    text_new_layer1 .contents = f"{text_from_user .upper()}"

    options = ps.JPEGSaveOptions(quality=5)
    jpg = r'mypath\photo.jpg'
    doc.saveAs(jpg, options, True)
except Exception as e:
    print(e)

If you run the function like this, nothing works

@bot.message_handler(content_types=['text'])
def func(message):
    if(message.text == '/ph'):    #Here we send a request to the function via telegram chat
        user_info = {'test' : 'asdsaasd'}
        test_edit_text(user_info) 

But if you run it like this, it works fine

if __name__ == '__main__':
     user_info = {'test' : 'asdsaasd'}
     test_edit_text(user_info) `

Why does the code suddenly stop seeing Photoshop? Is it possible that the two libraries are incompatible?

@loonghao
Copy link
Owner

loonghao commented Jul 5, 2022

Hi @emogothyy
We only have two places that have Please check if you have Photoshop installed correctly.
https://github.com/loonghao/photoshop-python-api/blob/main/photoshop/api/_core.py#L43
https://github.com/loonghao/photoshop-python-api/blob/main/photoshop/api/_core.py#L97

This API then relies on having Photoshop installed locally,
You can try to open Photoshop manually first, and then start your bot server to test.

If possible, please take a screenshot of your running process so that we can take a look

Why does the code suddenly stop seeing Photoshop? Is it possible that the two libraries are incompatible?

I checked that the dependencies between the two sides are not in conflict.

@emogothyy
Copy link
Author

@loonghao Thanks a lot! It really helped me to open Photoshop first with import os and then everything started working properly.

I'll attach screenshots now. If necessary, I can send all the files.

image

Thanks again

@emogothyy
Copy link
Author

image

@loonghao
Copy link
Owner

loonghao commented Jul 7, 2022

@emogothyy
By default, If your Photoshop is not opened, the PYPS API will automatically open Photoshop when it is executed.
You can try tweaking your code a bit, similar to the following.

# main.py

from photoshop import Session

@bot.message_handler(content_types=['text'])
def func_a(message):
    if(message.text == '/ph'):
        with Session() as ps:
            user_info = {'test' : 'func_a'}
            test_edit_text(user_info, ps_api=ps)



@bot.message_handler(content_types=['text'])
def func_b(message):
    if(message.text == '/ph'):
        with Session() as ps:
            user_info = {'test' : 'func_b'}
            test_edit_text(user_info, ps_api=ps)


# photoshop_function.py
def test_edit_text(info_from, ps_api):
    try:
        psApp = ps_api.Application()
        psApp.Open(r"mypath\first.psd")
        doc = psApp.Application.ActiveDocument
        print(info_from['test'])
        text_from_user = info_from['test'] 
        layer1init = doc.ArtLayers["layer1"]
        text_new_layer1 = layer1init.TextItem
        text_new_layer1 .contents = f"{text_from_user .upper()}"

        options = ps.JPEGSaveOptions(quality=5)
        jpg = r'mypath\photo.jpg'
        doc.saveAs(jpg, options, True)
    except Exception as e:
        print(e)

@emogothyy
Copy link
Author

I tried this method, but these places still have the same error, maybe the problem is in my photoshop, even after reinstalling, but if as before open with os or just open, everything works fine. Thanks so much for helping me with this error. Can I close the issue as solved?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants