loonghao / photoshop-python-api

Python API for Photoshop.
https://loonghao.github.io/photoshop-python-api/
MIT License
604 stars 68 forks source link

paste() won't print different images when the clipboard changes #269

Closed Wip-Sama closed 1 year ago

Wip-Sama commented 1 year ago

Describe the bug Using paste() to paste more than one image result in tha same image being printer over and over instead of the clipboard containing a different image

To Reproduce Steps to reproduce the behavior:

  1. select 2 different png images (raw_p, eng_p) (required win32clipboard and Pillow)

  2. Run this code

    
    import photoshop.api as ps
    from PIL import Image
    from io import BytesIO
    import win32clipboard

def send_to_clipboard(image: Image): output = BytesIO() image.convert('RGB').save(output, 'BMP') data = output.getvalue()[14:] output.close()

win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()

raw_p = r"path\to\1\img.png" eng_p = r"path\to\2\img.png"

raw_img = Image.open(raw_p) eng_img = Image.open(eng_p)

send_to_clipboard(raw_img) psapp = ps.Application() doc = psapp.documents.add(raw_img.width, raw_img.height, 72.0, "NewDoc") raw = doc.paste() raw.name = "raw" if eng_p != "" else "eng" raw.allLocked = True

if eng_p != "": send_to_clipboard(eng_img) eng = doc.paste() eng.name = "eng" eng.allLocked = True eng.visible = False

g1 = doc.layerSets.add() g1.name = "Clean"

g2 = doc.layerSets.add() g2.name = "Translation"

for layer in doc.layers: if layer.name == "Background": layer.remove()



**Expected behavior**
you will see that both: eng and row in the generated psd are the same image but if you open paint and simpli presst ctrl+v you will notice that the pasted image is different than the one in phosotop.

**Desktop (please complete the following information):**
 - OS: Windows10
 - Photoshop Version: Photoshop-2023
 - Python Version: 3.11

**Additional context**
I don't wanna use tmp file to add images to a psd if I can move everything in memory
loonghao commented 1 year ago

@Wip-Sama I have update the code to improve paste contents from clipboard.

Many times the binding behavior of COM is not as expected, we can solve it by executing JS code