Preset Crop 16 x 9

Moderator: Kathy_9

Post Reply
[Mike]
Posts: 48
Joined: Tue Jul 13, 2010 1:45 pm
operating_system: Windows 11
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte Z490 Aorus Elite AC
processor: Intel Core i5-10600K
ram: 32 GB
Video Card: NVIDIA GeForce RTX 3070
Monitor/Display Make & Model: LG 27GL850-B
Corel programs: PSP 2023

Preset Crop 16 x 9

Post by [Mike] »

I thought this was going to be an easy thing to record and make into a script, but I'm not getting anywhere. Maybe it's my very limited knowledge of scripting.

Basically, I have a bunch of different landscape images, with different dimensions. I want to crop the center part of each image and that center part must have a 16 x 9 ratio. So, the full width of the image and then the height sticking to the correct ratio. And the cropped part is in the center of the image, in other words, an equal part is cropped off the top and bottom.

PSP (2018) has this functionality built in. Just go to the crop tool and select the preset 16 x 9. You get a crop selection, with the correct ratio, full width (whatever the width of the image may be) and centered. So far so good.

But recording this and turning it into a script, does not work.

The built in preset looks like this:

Code: Select all

from PSPApp import *

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

def Preset_Crop():
    return {
        'CropRect': ((0,0), 1600, 900), 
        'Mode': App.Constants.CropMode.CustomAR, 
        'Units': App.Constants.CropUnits.Inches, 
        'SelectedArea': False, 
        'PrintWidth': -1, 
        'PrintHeight': -1, 
        'CropAsNewImage': True, 
        'RotationAngle': 0, 
        'AutoFit': True
        }

def Do(Environment):
    # Crop_LOCALIZED
    App.Do( Environment, 'Crop',         Preset_Crop())
Which is Preset_Crop_16 x 9.PspScript located in C:\Program Files\Corel\Corel PaintShop Pro 2018 (64-bit)\Languages\EN\Presets\. Using it as part of the built in list of crop presets via the crop tool works fine, using the above as a script, takes a 1600 x 900 bite out of the top left corner of any image.

Below is a recorded example:

Code: Select all

from PSPApp import *

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

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

    # Crop
    App.Do( Environment, 'Crop', {
            'CropRect': ((0,1151.41), 11387, 6405.19), 
            'Mode': App.Constants.CropMode.CustomAR, 
            'Units': App.Constants.CropUnits.Inches, 
            'SelectedArea': False, 
            'PrintWidth': -1, 
            'PrintHeight': -1, 
            'CropAsNewImage': False, 
            'RotationAngle': 0, 
            'AutoFit': False, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((20,0,0),1)
                }
            })
((0,1151.41), 11387, 6405.19) is true for the recorded image and proves the crop tool preset does the necessary calculations. But then using it on a different image with different dimensions, gives the same result, 11387 x 6405...

So, what does the built in tool do extra to get the right result each time? How does it go from the original text and 'CropRect': ((0,0), 1600, 900), to the proper location and size? :?:
Mike...
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: Preset Crop 16 x 9

Post by LeviFiction »

No idea

But here, I just added the calculations and I think it works as it should.

Code: Select all

from PSPApp import *

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

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((20,0,0),1)
                }
            })
    #Calculate Crop Rectangle Height from image height and width
    ar = 16/9.0
    nWidth = App.TargetDocument.Width
    nHeight = nWidth / ar
    #Place starting y position as half the image height minus half crop rectangle height 
    start = (0, (App.TargetDocument.Height/2) - (nHeight/2))
    # Crop
    App.Do( Environment, 'Crop', {
            'CropRect': (start, nWidth, nHeight), #Use calculated start, width, and height here
            'Mode': App.Constants.CropMode.CustomAR, 
            'Units': App.Constants.CropUnits.Inches, 
            'SelectedArea': False, 
            'PrintWidth': -1, 
            'PrintHeight': -1, 
            'CropAsNewImage': False, 
            'RotationAngle': 0, 
            'AutoFit': True, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((20,0,0),1)
                }
            })
https://levifiction.wordpress.com/
[Mike]
Posts: 48
Joined: Tue Jul 13, 2010 1:45 pm
operating_system: Windows 11
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte Z490 Aorus Elite AC
processor: Intel Core i5-10600K
ram: 32 GB
Video Card: NVIDIA GeForce RTX 3070
Monitor/Display Make & Model: LG 27GL850-B
Corel programs: PSP 2023

Re: Preset Crop 16 x 9

Post by [Mike] »

Hi LeviFiction,

I was thinking a calculation would have to be done somewhere, but it would've taken me a while to get it into a script, if at all. I'm very grateful for your contribution!

I have tested it on a number of images and compared the results with crops done via the crop tool itself and they are identical. So, this works extremely well!

Next step will be for me to resize the result to a 4K resoltuion, for which I have made a script already. Same aspect ratio, so no problem. And then I just need to trigger Save as and overwrite the original file (I have backups) and then I'm done. I'll try and share the end result over the weekend.

Thanks again!
Mike...
[Mike]
Posts: 48
Joined: Tue Jul 13, 2010 1:45 pm
operating_system: Windows 11
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte Z490 Aorus Elite AC
processor: Intel Core i5-10600K
ram: 32 GB
Video Card: NVIDIA GeForce RTX 3070
Monitor/Display Make & Model: LG 27GL850-B
Corel programs: PSP 2023

Re: Preset Crop 16 x 9

Post by [Mike] »

Or I can do the combined script in a batch process apparently, I need to play around with it. You've made some helpful posts elsewhere as well, LeviFiction. :wink:

Edit: I don't even need to combine the scripts, it allows me to run one after the other. Batch process works great! :D
Mike...
Post Reply