Script: to add border to layer

Moderator: Kathy_9

JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Script: to add border to layer

Post by JoeB »

EDIT: I just replaced the script with a slight change. I had not made the dialogue to set the border size Interactive so the previous one won't let you set the border size unless you had toggeled interactive mode. The replacement will let you do so regardless of the toggle. I have also added a second script that allows you to add a drop shadow after creating the border. That part is also interactive so that you can adjust the shadow offsets and color.

@pdxrjt asked about adding borders to layers because the Image>Add Borders option adds a border to the entire image while he/she wants to add borders to various smaller images added as layers in a collage. I believe the attached script will do the task. It uses Gary Barton's Find Selection Point script to locate the selection border to be filled.

THIS SCRIPT WILL ADD A BORDER TO SMALLER LAYER IMAGES REGARDLESS OF THEIR SHAPE OR ORIENTATION - I.E. THEY CAN BE HORIZONTAL/VERTICAL, ROTATED TO SOME DEGREE, OR NON-RECTANGULAR SHAPES. Also, if you have multiple layers with smaller images, if you first merge just all of those layers prior to running the script it will put the border on all of the images at once, so long as you are satisfied with having the same size and color of border for all such images.

You first have to have the layer you want to work on active in the Layers Palette then run the script. Basically, it uses the magic wand with Opacity selected to select the upper left pixel of the image which generally (when working with layers of a collage, with picture tube layers, etc.) will be transparent. Then it inverts the selection, creating a selection around the opaque part of the image, modifies that selection by Select Selection Borders, lets you set the border size, then fills the border with whatever your present foreground color is and finally de-selects the selection. You can then activate the next collage layer and run it again, etc.

This script will work on a collage image even if you have rotated the images in your collage to varying degrees, which is often the way people create collages. This script will also work on layers with irregularly shaped images - i.e., non-rectangular/square like, circles, ovals and those of some picture tubes, etc (although don't do it on tubes that have their own drop shadows as it will put the border around the shadow also). This gets around the limitation of using the Layer Opaque tool option which creates a vertical/horizontal rectangular border that is constrained by the size of the corners of the image and not around only the opaque rotated part of the image.

HINT: When running the script on non-rectangular images like picture tubes, or even an a rectangular image that is rotated in less than 90 degree increments, consider checking the Anti-alias box in the dialogue that sets the selection borders to achieve smoother results. Images rotated less than 90 degrees do have jaggy edges, and selections around irregular shapes are not smooth without anti-alias. With large images this is imperceptible but when zoomed in a lot you'll see that.

ONE NOTE: This script creates a selection border INSIDE the image, rather than outside. That is because when you create a selection border outside the image the outside corners of the border are not square. Creating an inside border creates square corners. With some editing this script could be modified to deal with the issue by making a copy of the image resized larger than the original by the number of pixels you want the border to be but I didn't feel like going that far. :-)

This script can also easily be modified to allow you to select the foreground color while the script is running. I have added an attached script that modifies the original to add a drop shadow after filling the border, which is often a nice effect doing a collage or with images added to a DVD cover or other artwork. If you use that version you can always cancel the drop shadow when that dialogue appears.

I just also noticed that it sets your Selection tool to Remove - easily fixed but just a warning the next time you decide to use your selection tools and think they aren't working. :-)

EDIT #2: I just replaced the two scripts (Dec. 28, 12:40pm MST)- NO change to how they work but just changed the name slightly to indicate they add borders to SMALLER layers (those added on top of a larger image), plus added explanations and instructions at the top of the scripts that can be read if opened with Edit mode.

PLEASE let me know if it doesn't work for you!
Attachments
Add Border to Smaller Layer.zip
Add border to smaller layer
(1.71 KiB) Downloaded 313 times
Add Border to Smaller Layer_Drop Shadow.zip
Add border to smaller layer with drop shadow.
(1.87 KiB) Downloaded 342 times
Last edited by JoeB on Thu Dec 31, 2015 2:48 am, edited 5 times in total.
Regards,

JoeB
Using PSP 2019 64bit
User avatar
gbotes
Posts: 104
Joined: Wed Sep 30, 2015 11:31 am
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
ram: 16GB
Hard_Drive_Capacity: 500Gb
Corel programs: Paint Shop Pro X8, Aftershot 2
Location: South Africa
Contact:

Re: Script to add border to layer

Post by gbotes »

Hey it sound interesting, JoeB. I'll give it a try later when my household chores are done. I have been wanting to do something like this myself but you beat me to it. :)
LeviFiction
Advisor
Posts: 6831
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 to add border to layer

Post by LeviFiction »

Me as well, but I'm actually making a full replacement for Add Borders so....I'll let my own slowness slide. xD This looks excellent JoeB.
https://levifiction.wordpress.com/
User avatar
gbotes
Posts: 104
Joined: Wed Sep 30, 2015 11:31 am
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
ram: 16GB
Hard_Drive_Capacity: 500Gb
Corel programs: Paint Shop Pro X8, Aftershot 2
Location: South Africa
Contact:

Re: Script to add border to layer

Post by gbotes »

Running the add border to layer script (without drop shadow) produced this error for me:

Traceback (most recent call last):
File "<string>", line 50, in Do
JascApp.NoSelectionDefined: No selection exists

Script 'Add Border to Layer' has completed with an error.

I got a similar error with the drop shadow version.
pdxrjt
Posts: 459
Joined: Tue Jul 22, 2008 4:30 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: Intel Core i7 - 4.2 GHz
ram: 32 GB
Video Card: NVIDIA GeForce GTX 1080
sound_card: NVIDIA High Definition Audio
Hard_Drive_Capacity: 10TB Int.
Monitor/Display Make & Model: Acer 32 inch x2
Corel programs: Paint Shop Pro 2018
Location: Portland, Oregon

Re: Script to add border to layer

Post by pdxrjt »

Thanks, it seems to work okay on my machine. Good script!
Adverts for Corel products when you close PSP don't bother me.
JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Script to add border to layer

Post by JoeB »

LeviFiction wrote:Me as well, but I'm actually making a full replacement for Add Borders so....I'll let my own slowness slide. xD This looks excellent JoeB.
I'm looking forward to your Add Borders replacement script. The reason I didn't try that route was because I just don't have the scripting knowledge to do it. :-) When you get it done it could easily be a better replacement for mine.

I have a couple of extra tweaks I'm thinking of adding just because I can see situations where they might be useful - e.g., adding a selection Smoothing interactive element to help smooth out the border outlines created on non-rectangular objects. But that's for when I have more time - another busy week ahead!
Regards,

JoeB
Using PSP 2019 64bit
User avatar
gbotes
Posts: 104
Joined: Wed Sep 30, 2015 11:31 am
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
ram: 16GB
Hard_Drive_Capacity: 500Gb
Corel programs: Paint Shop Pro X8, Aftershot 2
Location: South Africa
Contact:

Re: Script to add border to layer

Post by gbotes »

Since my PSP X8 refused to run those scripts, I offer these which I recorded today, and which may help others who encounter the error I did.

This script creates a 50 pixel border within the image:

Code: Select all

from PSPApp import *

def ScriptProperties():
    return {
        'Author': u'Grant Botes',
        'Copyright': u'2015. Please use, share and modify freely.',
        'Description': u'Created from command history with SaveHistoryTo'\
            u'QuickScript',
        'Host': u'PaintShop Pro',
        'Host Version': u'18.00'
        }


def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # LayerDuplicate
    App.Do( Environment, 'LayerDuplicate', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # SelectAll
    App.Do( Environment, 'SelectAll', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # Contract Selection
    App.Do( Environment, 'SelectContract', {
            'ContractAmount': 50, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # SelectInvert
    App.Do( Environment, 'SelectInvert', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # Cut
    App.Do( Environment, 'Cut', {
            'PerformCopy': True, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # Fill
    App.Do( Environment, 'Fill', {
            'BlendMode': App.Constants.BlendMode.Normal, 
            'MatchMode': App.Constants.MatchMode.RGBValue, 
            'Material': {
                'Color': (255,255,255), 
                'Pattern': None, 
                'Gradient': None, 
                'Texture': None, 
                'Art': None
                }, 
            'UseForeground': True, 
            'Opacity': 100, 
            'Point': (27.5,1314.5), 
            'SampleMerged': False, 
            'Tolerance': 0, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # SelectNone
    App.Do( Environment, 'SelectNone', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((18,0,0),1)
                }
            })

And this script adds a 75 pixel border around the outside of an image:

Code: Select all

from PSPApp import *

def ScriptProperties():
    return {
        'Author': u'Grant Botes',
        'Copyright': u'2015. Please use, share and modify freely.',
        'Description': u'Created from command history with SaveHistoryTo'\
            u'QuickScript',
        'Host': u'PaintShop Pro',
        'Host Version': u'18.00'
        }


def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # SelectAll
    App.Do( Environment, 'SelectAll', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # AddBorders
    App.Do( Environment, 'AddBorders', {
            'Bottom': 75, 
            'Left': 75, 
            'Right': 75, 
            'Symmetric': True, 
            'Top': 75, 
            'Color': (255,255,255), 
            'DimUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                }
            })

    # SelectNone
    App.Do( Environment, 'SelectNone', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((18,0,0),1)
                }
            })

JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Script to add border to layer

Post by JoeB »

gbotes wrote:Running the add border to layer script (without drop shadow) produced this error for me:

Traceback (most recent call last):
File "<string>", line 50, in Do
JascApp.NoSelectionDefined: No selection exists

Script 'Add Border to Layer' has completed with an error.

I got a similar error with the drop shadow version.
First, I'm not sure what you mean by running it to a layer script. Could you provide more details of exactly what you're doing?

This script is based on the assumption that people putting layers above another layer are doing so using layers with smaller opaque images so that some of the background can still be seen (creating collages, calendars, etc.). It therefore assumes that the first (upper left) pixel of the upper added layer will be transparent. The magic wand selects that pixel with Opacity and a tolerance of 26 and basically selects transparent pixels. Then the selection is inverted, creating a selection around the opaque part of the image. IF that first pixel is not transparent and there is no transparency within the tolerance of the selection then when the selection is inverted it will actually be deleted as there is nothing to invert to. So given where the script throws an error for you (line 50) it would appear that there is an original selection but then the selection disappears after the script inverts the original selection.

So given the way the script works, does the image on which you are running the script fit within the assumptions on which the script was built? If so, then I need more detail of exactly what you are doing to determing why it is throwing the error because it would appear that something else is happening to the image between the time the first selection is made and the selection is inverted.
Regards,

JoeB
Using PSP 2019 64bit
User avatar
gbotes
Posts: 104
Joined: Wed Sep 30, 2015 11:31 am
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
ram: 16GB
Hard_Drive_Capacity: 500Gb
Corel programs: Paint Shop Pro X8, Aftershot 2
Location: South Africa
Contact:

Re: Script to add border to layer

Post by gbotes »

Thanks for the explanation, JoeB. That makes sense. I was just referring to the name of the script - on my laptop it downloaded and saved as "Add border to layer". Your explanation makes it clear why the script failed on my side - no transparent pixels because I'm not working with collages :)
JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Script to add border to layer

Post by JoeB »

gbotes wrote:Since my PSP X8 refused to run those scripts, I offer these which I recorded today, and which may help others who encounter the error I did.
I've checked out your script and now realize that you may have misunderstood the purpose of my script, which is set out in the second paragraph of my original post. @pdxrjt was looking for a script to place borders around smaller opaque images layered on top of a larger background to create a collage with outlines around those smaller images, not a script to add borders to a full image which, I'm afraid it seems your script is aimed at.

That said, however, I ran your first script for the purpose you intended it and while it runs without throwing an error it does not fill the selection after the cut. The problem is in the Fill command, line 78. The integer 1315 should be 315. When I changed it to 315 it worked.

That said again, however, the script as written will not create a border smaller than 28 pixels because of the parameters set in line 78. Setting those parameters to 0.5,0.5 will allow a border as small as 1 pixel to be created on an image of any size.
Last edited by JoeB on Mon Dec 28, 2015 8:58 pm, edited 1 time in total.
Regards,

JoeB
Using PSP 2019 64bit
JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Script to add border to layer

Post by JoeB »

A post by @gbotes alerted me to the fact that some people might think, from the script's name, that it is meant to add borders to a layer duplicate of the full sized image rather than to add borders to layers with smaller images placed above a larger image such as is done when creating collages, calendars and similar projects. I have uploaded the scripts again with a slight name change that might help, and also added an explanation and instructions within the script itself which can be read in Edit mode. There is NO CHANGE to the way the scripts operate!
Regards,

JoeB
Using PSP 2019 64bit
pdxrjt
Posts: 459
Joined: Tue Jul 22, 2008 4:30 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: Intel Core i7 - 4.2 GHz
ram: 32 GB
Video Card: NVIDIA GeForce GTX 1080
sound_card: NVIDIA High Definition Audio
Hard_Drive_Capacity: 10TB Int.
Monitor/Display Make & Model: Acer 32 inch x2
Corel programs: Paint Shop Pro 2018
Location: Portland, Oregon

Re: Script to add border to layer

Post by pdxrjt »

I (cleverly) figured out it was for collages etc. and thank you as it works great for that on my machine! :D
Adverts for Corel products when you close PSP don't bother me.
JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Script to add border to layer

Post by JoeB »

pdxrjt wrote:I (cleverly) figured out it was for collages etc. and thank you as it works great for that on my machine! :D
I'm glad it does the trick for you - and me too, actually, because I do outlines on small images creating DVD or CD covers fairly often.

By the way, if you do a collage or similar project with a lot of smaller image layers, and if you don't mind all of them having the same border size and color, then once you have placed them to your satisfaction you can merge those smaller image layers into one layer (NOT with the background or other layers!) and then run the script just once on that merged layer. It will put the border around all of those images at once, saving you from running the script on each individual layer separately. Likely not too much of a time saver unless you have lots of small images, and easier to do if all of those layers are together in your Layers Palette so that you can turn off the visibility of all the other layers and then do Merge>Visible to get the small images on one merged layer.

AND once they're merged and bordered you're stuck with them! So best to duplicate your image first and them merge and apply the borders so that you still have the unmerged layers to save and work on later if necessary!
Regards,

JoeB
Using PSP 2019 64bit
User avatar
gbotes
Posts: 104
Joined: Wed Sep 30, 2015 11:31 am
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
ram: 16GB
Hard_Drive_Capacity: 500Gb
Corel programs: Paint Shop Pro X8, Aftershot 2
Location: South Africa
Contact:

Re: Script to add border to layer

Post by gbotes »

Hey, thanks @JoeB! I really appreciate your feedback because now you're helping me learn. Awesome! :D
JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: Script to add border to layer

Post by JoeB »

gbotes wrote:Hey, thanks @JoeB! I really appreciate your feedback because now you're helping me learn. Awesome! :D
Thanks, but unfortunately that was only a little bit of help, and I hope you don't mind a couple more comments.

The main problem with your first script is that, doing what it is intended to do, it creates a border inside the image. While this works fine for the purpose of my script (which is to provide a relatively small outline that will not cover any significant amount of the image) you want to create a relatively large border inside the image, and this border covers up a significant part of the image, often covering important image elements. So people wanting to create large borders will want to do so with the border outside the image. For that reason, I'm afraid that I don't think that first script will have many uses.

Your second script, while creating outside borders, is actually just running the Add Borders command which already exists in the Image menu. Also, running the Add Borders from the Image menu does not require that the image first be selected while your script adds the Select All command, which is just an extra step. And, of course, the existing Image>Add borders already allows a user to select both the size of the border and the color. So I'm afraid that the second script is just duplicating an existing PSP tool.

But keep at it! I'm not actually all that well versed in scripting either, and what little I do know is the result of both constant trying plus - and more important - input from scripters like Levi and those like Suz, Joske, Spandex and many others in the old Jasc newsgroups.
Regards,

JoeB
Using PSP 2019 64bit
Post Reply