Extending Python Libraries

Moderator: Kathy_9

Dave_SMR
Posts: 8
Joined: Fri Jul 29, 2016 10:08 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: FOXCONN A76ML-K 30 1.0
processor: Athlon II X4 640
ram: 8GB
Video Card: ATI Radeon 3000 Graphics
Hard_Drive_Capacity: 250GB
Monitor/Display Make & Model: Hanns.G HL225D x 2
Corel programs: PSP X7 / VS X8

Extending Python Libraries

Post by Dave_SMR »

Hi,

I've just started digging a little deeper into some inherited pspscripts and I've come to realise that I could (probably) consolidate various processes under one script.

To completely generate a one script solution I need to be able to write to a database from the script directly. Can anyone give me advice on how to add pyodbc (or similar) to the libraries available to the PSP python interpreter?

Dave
LeviFiction
Advisor
Posts: 6774
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: Extending Python Libraries

Post by LeviFiction »

PSP's implementation of Python doesn't often lend itself to adding third party libraries easily. For example I've never managed to get PIL or Pillow to install or compile to a version that works. There's always an error that it can't find the libraries or other such errors. I think it's because of the custom compiled DLL style libraries that Python has to access.

However, simple libraries that are pure python are easy to add.

Now, PSP's python does come with all of the standard libraries including sqlite3. So if that's sufficient for your purposes I'd suggest just using it.

Otherwise, technically you need to place any third-party libraries in the site-packages folder. This is located at C:\Program Files\Corel\PaintShop Pro X8\Python Libraries\Lib\Site-Packages

Or Program Files (x86) if you are using the 32-bit version on a 64-bit machine. And of course whatever version of PSP you're using.
Dave_SMR
Posts: 8
Joined: Fri Jul 29, 2016 10:08 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: FOXCONN A76ML-K 30 1.0
processor: Athlon II X4 640
ram: 8GB
Video Card: ATI Radeon 3000 Graphics
Hard_Drive_Capacity: 250GB
Monitor/Display Make & Model: Hanns.G HL225D x 2
Corel programs: PSP X7 / VS X8

Re: Extending Python Libraries

Post by Dave_SMR »

Thanks for the reply and advice Levi.

I hadn't considered utilising sqlite3, it is certainly a feasible solution, although it will require some re-factoring elsewhere.

You statement
LeviFiction wrote:However, simple libraries that are pure python are easy to add.
really is the solution. I got so buried into trying to deploy the setup.py for pyodbc I didn't think to look for a pure Python solution. Surely enough pypyodbc is what I am after.

I'll sink my teeth into it on Monday and let you know how it goes but which ever solution I go with you have helped cure my woes. Thank you.
Dave_SMR
Posts: 8
Joined: Fri Jul 29, 2016 10:08 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: FOXCONN A76ML-K 30 1.0
processor: Athlon II X4 640
ram: 8GB
Video Card: ATI Radeon 3000 Graphics
Hard_Drive_Capacity: 250GB
Monitor/Display Make & Model: Hanns.G HL225D x 2
Corel programs: PSP X7 / VS X8

Re: Extending Python Libraries

Post by Dave_SMR »

The pypyodbc works a treat. Thanks again.
ilgk48
Posts: 63
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: Extending Python Libraries

Post by ilgk48 »

LeviFiction wrote: Fri Jul 29, 2016 12:48 pm PSP's implementation of Python doesn't often lend itself to adding third party libraries easily. For example I've never managed to get PIL or Pillow to install or compile to a version that works. There's always an error that it can't find the libraries or other such errors. I think it's because of the custom compiled DLL style libraries that Python has to access.
Hi LeviFiction, I need, from the TargetDocment, to get the dimensions of another document without opening it.
Looking around for getting a Python code, the first thing I found is the PIL library, but It seems that you have had problem with it.
So, there is a simpler way?

Thank you-
LeviFiction
Advisor
Posts: 6774
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: Extending Python Libraries

Post by LeviFiction »

What type of file?

I can think of at least 3 different options. None of them particularly easy, definitely not easier than Pillow. But they are do-able. There are probably better suggestions but I don't know what they would be.

1) Find a library that uses pure python. Pillow is complied and not pure python so it's not currently an option for us (unless someone wants to work out how to compile it for PSP specifically). Depending on what types of files you'll be reading you may need to have multiple libraries to handle them all.

Piexif - https://pypi.org/project/piexif/#description can read TIFF, jPEG, and webP formats.
pyPNG - https://github.com/drj11/pypng can read PNG files

Both are pure python and should run just fine from PSP. Though I have never used them.

2) Create/use a separate program that can read the data and pass it back to you. There are several options to do this but I'm not familiar with them. I never do this. But I imagine it would be quite easy to install the full version of Python on your computer, create a program that can read image dimensions that uses PIL and have it put those dimensions in a file, or output them to a subprocess. Again I don't have details on how this would work, but if you can get another program to do the hard work of parsing the files for you, all you have to do is figure out how to pass that information back to PSP. The existing NIK scripts would be a good starting point.

3) Parse the file format for yourself. - This is the hardest of my three ideas. It requires learning how different image formats are written, and then using the built-in libraries in Python to parse the format. I have several scripts that do this sort of thing for different file types like PNGs, PSDs, and PSPImage files. You can use the built-in binary reading option to read in the files, and then use the struct library to transform the binary values into something readable.

I learned the basics of doing this from articles like this one - https://www.devdungeon.com/content/work ... ata-python
ilgk48
Posts: 63
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: Extending Python Libraries

Post by ilgk48 »

Thank you Levi,
In the meanwhile I've installed (pure) Python 2.7 and I got no problem (apparently) wit PIL; te fact is that PSP uses, as you say, a customized Python that unlike the pure Python does not like an instruction like;

Code: Select all

 from PIL import image
In sys.path I added the reference to PIL but the module image abort when tries to:

Code: Select all

  File "C:\Python27\Lib\site-packages\PIL\Image.py", line 90, in <module>
    from . import _imaging as core
I'll study the pure Python library that you have mention.
I''ll let you know.
Thank you.
Paolo

PS. Forgotten: i treat Jpg images.
ilgk48
Posts: 63
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: Extending Python Libraries

Post by ilgk48 »

Hi LeviFiction,
I have successfully applied your suggestion number two :D :D :D

The external program is a Microsoft Vb.Net Application, consisting in very few lines, that gets the rating, width and height of a jpg image. Then, within a PSPscript, I call it using subprocess library.
This is the fragment of code:

Code: Select all

from PSPApp import *
import sys
import subprocess
...... omissis

# This does my stuff: returns in a list the path (complete, head) of a image that is correlated to the Target Document 
# according to my criteria irrilevant here.
#[0]: complete path
#[1]: head of the path
#[2]: boolean, the correlated image has been found/not found
pathOfTheOriginalDocumentInfo = PSPmyGlobal.myPathOfTheOriginalDocumentInfo(App.TargetDocument)

#If image has been found, calls a my VB.NET Windows application that returns Path, Rating, Width and Height of the correlated image,
#joined in a string, and separated each other with ";".
#As to say: Path;Rating;Width;Height
#The string is then trasposed in a list
if pathOfTheOriginalDocumentInfo[2] == True:
    myProcessExe        = 'C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\ReadJpgArgs\\ReadJpgArgs\\bin\\Debug\\ReadJpgArgs.exe' 
    myProcessCmd        = myProcessExe + ' -f ' + '"' + pathOfTheOriginalDocumentInfo[0] + '"'
    myProcessOut        = subprocess.Popen(myProcessCmd, stdout=subprocess.PIPE)
    myLine              = myProcessOut.stdout.readline().rstrip()
    myImageJpgArgs      = myLine.split(';')
else:
    myImageJpgArgs      = [None] * 4
    
#For debugging purposes
print  myImageJpgArgs[0]
print  myImageJpgArgs[1]
print  myImageJpgArgs[2]
print  myImageJpgArgs[3]

... contine
 
For my personal use this is enough quite right.
Anyway, the next step is the using of pure Python (and the PIL library) for building myProcessExe and myProcessCmd strings.

Paolo
ilgk48
Posts: 63
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: Extending Python Libraries

Post by ilgk48 »

LeviFiction wrote: Mon Apr 19, 2021 6:55 pm
2) Create/use a separate program that can read the data and pass it back to you. There are several options to do this but I'm not familiar with them. I never do this. But I imagine it would be quite easy to install the full version of Python on your computer, create a program that can read image dimensions that uses PIL and have it put those dimensions in a file, or output them to a subprocess. Again I don't have details on how this would work, but if you can get another program to do the hard work of parsing the files for you, all you have to do is figure out how to pass that information back to PSP. The existing NIK scripts would be a good starting point.
Hi LeviFiction,
I've already post a solution that calls a my personal VB.NET Application for getting width and height of a jpeg without opening it in PSP GUI.
Here is my solution in Pyhon.
The lamp in my brain blinked when you wrote (see above quoted) "subprocess" :) :) :)
I installed Python 2.7 (also for Window) on my PC and using subprocess in the PSPscript I call Python 2.7 for managing the PIL library (that has to be installed apart).
So in my PSPScript I have this code (similar to the one used for calling the VB.NET application):

Code: Select all

 
if pathOriginalDocumentInfo[2] == True:
    myProcessExe = 'C:\Python27\Pythonw.exe'
    myProcessSpy = 'C:\Python27\myScripts\ReadJpgArgs.py'
    myImagePath  = pathOriginalDocumentInfo[0]
    myProcessCmd = [myProcessExe, myProcessSpy, myImagePath]
    myProcessOut    = subprocess.Popen(myProcessCmd, stdout=subprocess.PIPE)
    myLine          = myProcessOut.stdout.readline().rstrip()
    myImageJpgArgs  = myLine.split(';')
else:
    myImageJpgArgs      = [None] * 2  
    
#For debugging purposes
print  myImageJpgArgs[0]
print  myImageJpgArgs[1]
 
In the above solution i renounce to get Path and Rating of the image (but it not essential).
The previous Vb.Net application (see my previous post) now has become this Python module (named ReadJpgArgs.py):

Code: Select all

import sys
from PIL import Image

myImagePath = sys.argv[1]
myImage = Image.open(myImagePath)
width, height = myImage.size
myImageArgs = str(width) + ';' + str(height)
print myImageArgs
All simpler than VB.NET and all working for me :D
Paolo
migf1
Posts: 510
Joined: Fri Apr 03, 2020 3:09 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit

Re: Extending Python Libraries

Post by migf1 »

Hi Paolo,

just wanted to add that since you are simply calling an external program from within the PSP script, the external program can be anything at all. For example, it doesn't have to be python 2.7, it could be the latest version of python.

Not essential, just letting you know.
Good Unofficial PaintShop Pro Tutorials: Creation CasselMake Shop ProHEC Image EditingLeviFiction PSP Basics
(plus my own Gimp & Stuff)
ilgk48
Posts: 63
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: Extending Python Libraries

Post by ilgk48 »

:) Yes! Thank you.
In effect I was a little perplexed.
I'm not a "pythoneer" and it shows!
Externally I call python 2.7 for a question of compatibility, useless if I should test some code outside PSP (he was my son that suggested me so).
Paolo
LeviFiction
Advisor
Posts: 6774
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: Extending Python Libraries

Post by LeviFiction »

Could also call ExifTool if you wanted to download it. Though that has less control over the output and you have to search through it to find what you want.