Add text from filename

Moderator: Kathy_9

Post Reply
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Add text from filename

Post by terrypin »

My novice attempts have so far foundered so I'd much appreciate some help please!

I want to operate on JPG photos, with the filename added as central text.

Typically the files will be simply numbered: 1.jpg, 2.jpg, 17.jpg, etc. Or PH1, PH2, etc, with the number never greater than two digits.

Applying the script to 17.jpg open (in ageing PSP 8 in my case) would give the result:

Image

I've used Tahoma white text with a black drop shadow in my example. I chose size 240 to make its height about a third the height of the image. But if that's one complication too many I'd happily settle for a fixed size giving roughly that appearance. And I could edit a script to experiment with detail settings.

I'm guessing that there's software around that does this sort of thing, but it would be so much more satisfying to achieve it within PSP itself.

Terry, East Grinstead, UK
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Add text from filename

Post by LeviFiction »

I think the text command in PSP 8 is a little different from newer versions. Can you post a pre-recorded version of your steps?

Technically the script itself is very easy. First, you just record the normal steps of a regular script.
1) Add Text of size 1/3rd the height of the image
2) Center in canvas
3) Add Drop shadow

Next, you open the script in a text editor and create some variables just under the "def Do(Environment):" section. These variables will be used to grab the Document Height and multiply it by one third to get the size. Make sure to round. I set a starting point of the center of the image just in case you ever have to deal with a smaller image.

Code: Select all

def Do(Environment):
    Size = int(round(App.TargetDocument.Height/3.0)) 
    Start = (App.TargetDocument.Width/2, App.TargetDocument)
    Text = App.TargetDocument.Title #grabs the name without the extension
    #...the rest of the script is here
Then inside the text command simply replace the Size, Start, and Text/Characters parameters with the appropriate variables as seel above

That should be all you need. It should then always create text 1/3rd the size of your image height using the Title of the image (as found in the title bar), center in the middle of the canvas, and then add a drop-shadow. I'm using 2018 so I don't know if the text command will directly transfer or not.
https://levifiction.wordpress.com/
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Re: Add text from filename

Post by terrypin »

Many thanks, looks very promising. I'll tackle it over the weekend.

Here's the (incredibly lengthy!) recorded script generated by placing some arbitrary text into an image. I've uploaded it as a TXT file to avoid possible difficulties.

https://www.dropbox.com/s/zpaeq1uqhp98y ... 8.txt?dl=0
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Add text from filename

Post by LeviFiction »

Small correction, I forgot Title does include the extension. So you'll just have to do a small split to get the name by itself. But it's very easy.

Code: Select all

Text = App.TargetDocument.Title.rsplit('.',1)[0]
I'm uploading my completed version of the script using your Text command (with a few edits of course). In my test, it seemed to work alright. You'll have to determine that for yourself of course.
Attachments
nameonImage.zip
(1 KiB) Downloaded 179 times
https://levifiction.wordpress.com/
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Re: Add text from filename

Post by terrypin »

Thanks for the full script which I installed first thing this morning. Haven't been able to spend much time working on it yet but on first run I saw this message in the Script Output:

Executing FileOpen
Executing FileClose
Executing FileClose
Executing FileClose
Executing FileClose
Executing RunScript
Executing EnableOptimizedScriptUndo
Traceback (most recent call last):
File "C:\Users\terry\My PSP8 Files\Scripts-Trusted\nameonImage.PspScript", line 22, in Do
Title = u'' + App.TargetDocument.Title.rsplit('.',1)[0]
AttributeError: 'str' object has no attribute 'rsplit'

(BTW, is that truncated on the right? I'm guessing 'in Do' should continue as 'in Document....'? But using the Copy tool does not get anything more than the above onto the clipboard.)

I haven't yet fathomed the meaning of line 22, much less understood what's apparently wrong with it.

In case it helps I've uploaded a screenshot of your script tiled alongside another that I've been using for years. Maybe some of the 'header' data needs adjusting?

https://www.dropbox.com/s/dap37yup5un9o ... .jpg?raw=1

Hope to get back on the case later this afternoon or evening.

Much appreciate your help and patience on this!

EDIT:
Pleased to report that before leaving it I did some googling with 'python attribute split' and tried replacing your 'rplit' by 'split' - with success! Text was placed, although no drop shadow, with this script output:
Traceback (most recent call last):
File "C:\Users\terry\My PSP8 Files\Scripts-Trusted\nameonImage-2.PspScript", line 88, in Do
App.Do( Environment, 'DropShadow', {
NameError: global name 'False' is not defined

I then recorded script while adding drop shadow to the pre-selected text. That gave this:

Code: Select all

from JascApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.10'
        }

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            
            })

    # Drop Shadow
    App.Do( Environment, 'DropShadow', {
            'Blur': 2, 
            'Color': (0,0,0), 
            'Horizontal': 12, 
            'NewLayer': App.Constants.Boolean.false, 
            'Opacity': 100, 
            'Vertical': 12, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })
So I replaced your line

Code: Select all

'NewLayer': False, 
by

Code: Select all

'NewLayer': App.Constants.Boolean.false,
... and it worked.

Thanks a bunch, that's going to save me a lot of work! :)
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
Post Reply