Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gimp Python-fu 2.7 to launch Python 3 for editing images
#1
Lightbulb 
On windows 10 you can open up python-fu console in gimp and launch a python 3 terminal.

Code:
from subprocess import call
call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe"])
If you have python 3.6 that will launch it and when you close that console it gets an exit code back. If you close python 3 by typing exit() the exit code is 0. If you just close the terminal you get -1073741510.

But if I try to pass in additional arguments about what python file to run then I get problems. For example if I run this.
Code:
from subprocess import call
call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe", "D:/Projects/neural-style-master/neural_style.py"])
I get an exit code of 1 and the terminal opens but immediately closes.

If I were to run the same thing by running python 2.7 and then run this.
Code:
from subprocess import call
call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe", "D:/Projects/neural-style-master/neural_style.py"])
Then from python 2.7 it launches the specified script using python 3.6. By then typing exit() I end up back in python 2.7.
----------------------------------------------------------------
But Why
----------------------------------------------------------------
The reason I want to do all this is so that I can write a plugin that sends a specified image or set of images to a command line program written for python 3.6. I would then either directly pass back the modified images or save them to a temp file, and then pass back the path.

The reason I need it to be python 3 is because of the use of Tensorflow and similar deep learning image processing libraries. Tensorflow both has useful projects written for it and approaches being easy to install on Windows, Mac, and Linux but it needs python 3.6.

The reason Tensorflow would be useful is there are projects like these Github projects.
https://github.com/anishathalye/neural-style
takes about 5 minutes to process an image with a gpu.

https://github.com/Eyyub/tensorflow-pix2pix
takes about 20 seconds to initialize and then about 2 seconds to process each image.

https://github.com/lengstrom/fast-style-transfer
takes 2 seconds to under a second to process an image.

All of which can make a wide range of innovative changes to images.
Integrating them into a program like gimp would greatly aid their integration into a broader creative process. As it stands now using something like neural-style requires a command line argument like...

D:/Projects/neural-style-master/neural_style.py --network D:/Projects/neural-style-master/imagenet-vgg-verydeep-19.mat  --checkpoint-output "D:/Projects/neural-style-master/temp/temp.png" --iterations 600 --style-scales 1.0 --content-weight-blend 1.0 --content-weight 5.0 --style-weight 500.0 --tv-weight 100.0 --pooling avg --width 512 --content "D:/tile/nature256/nature_00312.png" --styles "D:/tile/nature256/nature_01111.png" --output "D:/tile/temp/nature_00312.png_nature_01111.png.png"

There are efforts to integrate projects like these into a gui but it would be far preferable to make it into a plugin for something like gimp.
---------------------------------------
If what I am trying to do is not possible I am open to trying to do it another way.

The most extreme solution I can think of would be adding a python 3 version of python-fu.
I read somewhere that it was very important to keep python-fu as 2.7 because of plugin comparability. That seems very reasonable.
The same poster however said that there was nothing to stop adding python-fu 3 as a separate plugin. According to them the only reason it was not done already was because there was no great interest from the community to work on adding that feature.
Reply
#2
Possibly a matter of path. What do you get with:

Code:
call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe", "-c","import sys;print(sys.path);"])
Your current directory is another possible difference.
Reply
#3
(11-12-2018, 02:43 PM)Ofnuts Wrote: Possibly a matter of path. What do you get with:

Code:
call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe", "-c","import sys;print(sys.path);"])
Your current directory is another possible difference.

Thank you for your swift reply. That did return an exit code of 0 but it was too fast for me to see anything so I added a timer.

Code:
subprocess.call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe", "-c","import sys;print(sys.path);import time; time.sleep(20);"])

Code:
['', 'C:\\Program Files\\GIMP 2\\32\\lib\\gimp\\2.0\\python',
'C:\\Program Files\\GIMP 2\\lib\\gimp\\2.0\\plug-ins\\python-console',
'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\audov\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']

(I added the line breaks for visual clarity.)

I don't know yet if this will lead to the answer but I hope this way I can at least see more meaningful error codes.
Reply
#4
Now you have to compare the path between Gimp and non-Gimp.
Reply
#5
(11-12-2018, 04:18 PM)Ofnuts Wrote: Now you have to compare the path between Gimp and non-Gimp.

https://stackoverflow.com/questions/1016...ot-on-path

it is missing
C:\Users\audov\AppData\Local\Programs\Python\Python36\Scripts\
and
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
and I will probably add the path to this program to it while I am at it.
that stackoverflow seems to show how to do it but I don't have to time to test it now.
Reply
#6
Just a matter of:

Code:
import sys
sys.path.insert(0,r'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin')
Reply
#7
Sad 
I still plan to go back to it at some point. It will only become easier to get working as the libraries support more things.

Basically I was trying to launch an install of python 3 that was on my computer from gimp's built in python 2.7. This works just fine.

I would launch it with a command line argument to do the work. This also works just fine.

It would save some file in a folder for gimp python 2.7 to pass back to the plugin. I did not get this far but no reason to think it would not work fine.

In the end it would have a gui with sliders like are easy to make for plugins in gimp. There are like 10 sliders and 4 or so Boolean choices but that would not take too long really.

The sticking point was trying to get all the system variables for python 3 to be correct. The built in 2.7 python has modified variables. It is easy to modify these variables but it is basically impossible to reload those variables once gone.

In theory before launching python 3 I could change all those variables back to what they are for the rest of the system and then launch python 3 so it inherits those variables, or directly set them as a variable when launching python 3. But I never got it working. Tensorflow has a number of dependencies. Cuda, cudnn, numpy, windows visual c++ 2015 tools and more.

As a side note some of this tech is actually starting to get worked into commercial products, though currently it is done with a server processing the images for the user.

one of the code stubs I have saved from when I was working on this a lot is this. I would then try to import tensorflow.


import os, subprocess, sys

d = "environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': .... all the rest of the environment variable})"

subprocess.Popen(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe"], env=d, shell=True)
Reply
#8
(01-16-2019, 06:21 PM)audovoice Wrote: The sticking point was trying to get all the system variables for python 3 to be correct. The built in 2.7 python has modified variables. It is easy to modify these variables but it is basically impossible to reload those variables once gone.

In theory before launching python 3 I could change all those variables back to what they are for the rest of the system and then launch python 3 so it inherits those variables, or directly set them as a variable when launching python 3. But I never got it working. Tensorflow has a number of dependencies. Cuda, cudnn, numpy, windows visual c++ 2015 tools and more.

Still it is a a mere piece of code... It has inputs (environment variables or program parameters, maybe files), and I don't see what you couldn't emulate in an intermediate layer if you knew what the Python3 stuff needs.

(01-16-2019, 06:21 PM)audovoice Wrote: In the end it would have a gui with sliders like are easy to make for plugins in gimp. There are like 10 sliders and 4 or so Boolean choices but that would not take too long really.

No need for Gimp though, you can make a GUI interface in Python and create a standalone app.

Ps: See here how you can reroute the stdout/stderr of your Python process to check the output of Python errors, as well as your debugging print calls/statements.
Reply


Forum Jump: