[Photoshop][Python] Selecting regions: how to pass arrays

I’m trying to make a selection in photoshop and can’t figure out how to get the right type of data to the Select method.

The VBScript docs say:

Select
(Region Array (Points: Array (Array (x,y),…)
[, Type]
[, Feather]
[, AntiAlias])

I can’t figure out how to give it a Region that it will accept:

    psApp = win32.dynamic.Dispatch('Photoshop.Application')
    doc = psApp.Documents(1)
    doc.Selection.Select((0, 0, 512, 512))

I get: pywintypes.com_error: (-2147352567, ‘Exception occurred.’, (0, u’Adobe Photoshop’, u’Illegal Argument’, None, 0, -2147024809), None)
If I nest the region:

    psApp = win32.dynamic.Dispatch('Photoshop.Application')
    doc = psApp.Documents(1)
    doc.Selection.Select( ((0, 0), (512, 512)) )

I get: [I]pywintypes.com_error: (-2147352567, ‘Exception occurred.’, (0, u’Adobe Photoshop’, u’Illegal argument - argument 1

  • Only arrays with dimension 1 are supported’, None, 0, -2147220262), None)[/I]

in js, this works:

app.activeDocument.selection.select([[0, 0], [0,10], [10, 10], [10, 0]]);

You’ll need an array of 4 tuples for top left, top right, bottom right, bottom left points

That doesn’t work either, it wants a 1-dimensional array according to the error, and then throws an “Illegal Argument” error if I flatten it.

I think the issue is getting win32com to convert the list to the correct datatype so the method can understand it, but I’m not sure how to go about that (or why it isn’t happening automatically)

WinCom32 has problems passing arrays properly, try comtypes.

There’s some info about this in the comments on Adam’s page, this is how I found out about the issue…

-Chris

I had this problem as well, moving over to comtypes fixed it.

Great. Thanks guys!