Script from Photoshop can be useful in PSP?

Moderator: Kathy_9

zman85
Posts: 2
Joined: Tue Jul 13, 2021 2:05 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte B460M AORUS PRO
processor: i7-10700
ram: 16GB
Hard_Drive_Capacity: 8TB
Monitor/Display Make & Model: 3x iiyama ProLite X2783HSU
Corel programs: PSP Pro 2021

Script from Photoshop can be useful in PSP?

Post by zman85 »

I would like to get what I did in Photoshop. Namely, automatic processing of several thousand files at the same time:
- automatic removal of the white background (selecting the upper left pixel)
- changing the size of the working area to obtain a square with sides equal to the longer side of the processed file
- of course, saving the processed file

Is it possible in PaintShop Pro?
If so, would anyone be able to help me with this, how to do this automation?



On PSD i using this:
var docRef = app.activeDocument;

//if landscape and biger then newSize, resize using width
if (docRef.height < docRef.width)
{
//resizeImage(width, height, resolution, resampleMethod)
//docRef.resizeImage(newSize,null,null,ResampleMethod.BICUBIC);
//resize canvas to make image square
//resizeCanvas(width, height, anchor)
docRef.resizeCanvas(null,docRef.width,AnchorPosition.MIDDLECENTER);
}
//else resize as portrait if hight is bigger then newSize
else if (docRef.height > docRef.width)
{
//resizeImage(width, height, resolution, resampleMethod)
//docRef.resizeImage(null,newSize,null,ResampleMethod.BICUBIC);
//resize canvas to make image square
//resizeCanvas(width, height, anchor)
docRef.resizeCanvas(docRef.height,null,AnchorPosition.MIDDLECENTER);
}
User avatar
Ken Berry
Site Admin
Posts: 22123
Joined: Fri Dec 10, 2004 9:36 pm
operating_system: Windows 11
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte B550M DS3H AC
processor: AMD Ryzen 9 5900X
ram: 32 GB DDR4
Video Card: AMD RX 6600 XT
Hard_Drive_Capacity: 1 TB SSD + 2 TB HDD
Monitor/Display Make & Model: Kogan 32" 4K 3840 x 2160
Corel programs: VS2022; PSP2023; DRAW2021; Painter 2022
Location: Levin, New Zealand

Re: Script from Photoshop can be useful in PSP?

Post by Ken Berry »

I've moved your post to the Scripting sub-forum of PaintShop Pro for the obvious reason that it is about scripting!
Ken Berry
LeviFiction
Advisor
Posts: 6472
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: Script from Photoshop can be useful in PSP?

Post by LeviFiction »

Yes all of those steps should be possible. PSP uses Python for its scripting instead of JavaScript. Commands can be performed on documents but documents don't have built in member functions for this purpose. We have API commands we pass documents into if need be, or that just work on the currently active document.

If we only convert the script you posted it'll look something like this.

Code: Select all

from PSPApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'12.00'
        }

def Do(Environment):
    #TargetDocument is the document that was active when you ran the script, ActiveDocument is the one currently active in the UI
    docRef = App.TargetDocument 
    width = docRef.Width #used to set new width and height if any
    height = docRef.Height
	
    #if landscape and biger then newSize, resize using width
    if (height < width):
        # set new Height to match Width
        height = docRef.Width
    #else resize as portrait if hight is bigger then newSize
    elif (height > width):
        # set new Width to match Height
        width = docRef.Height
    # Resize Canvas with new width and height.  If no change is required nothing will happen
    # The last parameter of any App.Do() command is the document to perform it on.  If none
    # is given it'll use the ActiveDocument automatically.  So it's not necessary for me to 
    # use it, but I did anyway.

	# ResizeCanvas
    App.Do( Environment, 'ResizeCanvas', {
            'AspectRatio': width/height, 
            'FillColor': (0,0,0), 
            'HoriPlace': App.Constants.HorizontalType.Center, 
            'MaintainAspect': False, 
            'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'NewHeight': width, 
            'NewWidth': height, 
            'VertPlace': App.Constants.VerticalType.Center, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((12,0,0),1)
                }
            }, docRef)
I can shorten this slightly using Python's closest equivalent to a ternary operator. That looks like this

Code: Select all

from PSPApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'12.00'
        }

def Do(Environment):
    #TargetDocument is the document that was active when you ran the script, ActiveDocument is the one currently active in the UI
    docRef = App.TargetDocument 
    width = docRef.Width if docRef.Width > docRef.Height else docRef.Height
    height = docRef.Height if docRef.Height > docRef.Width else docRef.Width

    # Resize Canvas with new width and height.  If no change is required nothing will happen

	# ResizeCanvas
    App.Do( Environment, 'ResizeCanvas', {
            'AspectRatio': width/height, 
            'FillColor': (0,0,0), 
            'HoriPlace': App.Constants.HorizontalType.Center, 
            'MaintainAspect': False, 
            'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'NewHeight': width, 
            'NewWidth': height, 
            'VertPlace': App.Constants.VerticalType.Center, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((12,0,0),1)
                }
            })
I would like to note that the script you posted won't do everything you asked in PSP. In PSP we first need to promote the background layer to a regular raster layer. Then we need to select all of the white pixels, and clear them. Finally, resizing the image to be a square, and producing the SaveAs dialog.

In the following example I include a helper library called PSPUtils to find out if the currently selected layer is a background layer or not. If it is I promote the layer to a regular raster before continuing on. I also assume the top left color will be white otherwise the steps for selecting the color and hitting "Clear" won't work out well.

That would look something closer to this

Code: Select all

from PSPApp import *
import PSPUtils

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'12.00'
        }

def Do(Environment):
    #TargetDocument is the document that was active when you ran the script, ActiveDocument is the one currently active in the UI
    docRef = App.TargetDocument 
    if PSPUtils.LayerIsBackground(Environment, docRef): #taken from utility library
        # LayerPromoteBackground
        App.Do( Environment, 'LayerPromoteBackground', {})

    # Select White pixel in top left corner and clear
    # Magic Wand
    App.Do( Environment, 'MagicWand', {
            'General': {
                'Mode': App.Constants.SelectionOperation.Add, 
                'Antialias': False, 
                'Feather': 0, 
                'SampleMerged': False
                }, 
            'MatchMode': App.Constants.MatchMode.RGBValue, 
            'Contiguous': True, 
            'Point': (0,0), 
            'Tolerance': 20, 
            'AntialiasType': App.Constants.AntialiasType.Outside, })

    # ClearSelection
    App.Do( Environment, 'ClearSelection', {})

    #calculate resize

    width = docRef.Width if docRef.Width > docRef.Height else docRef.Height
    height = docRef.Height if docRef.Height > docRef.Width else docRef.Width

    # Resize Canvas with new width and height.  If no change is required nothing will happen

	# ResizeCanvas
    App.Do( Environment, 'ResizeCanvas', {
            'AspectRatio': width/height, 
            'FillColor': (0,0,0), 
            'HoriPlace': App.Constants.HorizontalType.Center, 
            'MaintainAspect': False, 
            'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'NewHeight': width, 
            'NewWidth': height, 
            'VertPlace': App.Constants.VerticalType.Center, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((12,0,0),1)
                }
            })

    # Call FileSaveAs.  You can also use just use FileSave to overwrite the original file but I'm assuming
    # You don't want to do that.
    
    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
            'Encoding': {}, 
            'FileName': docRef.Name, 
            'WorkingMode': 0, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Interactive, 
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                'Version': ((12,0,0),1)
                }, 
            'DefaultProperties': []
            })

Finally, if you remove the FileSaveAs command entirely from the script. You can use the script in a Batch Process and let the batch process handle the saving for you.

Here is Corel's tutorial on using Batch Process - https://www.paintshoppro.com/en/tips/bu ... it-images/
zman85
Posts: 2
Joined: Tue Jul 13, 2021 2:05 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte B460M AORUS PRO
processor: i7-10700
ram: 16GB
Hard_Drive_Capacity: 8TB
Monitor/Display Make & Model: 3x iiyama ProLite X2783HSU
Corel programs: PSP Pro 2021

Re: Script from Photoshop can be useful in PSP?

Post by zman85 »

LeviFiction You are amazing!
Thanks for converting my script.

I have one more question, how can i CROP just item from image with transparent background?
I need after MagicWand and before ClearSelection -> do that: Reverse selection + Crop to selection.

Despite the fact that I have been using PSD for over 10 years and after this period I receive information that my copy is probably illegal and that they block me from accessing the software (software is fully legal, boxed software purchased on VAT invoice in the store), I think that your very quickly I will switch to PSP forgetting about PSD :)