How do I add a library to RhinoPython?

I am interested in learning how to access non-standard python libraries from within RhinoPython (not GH Python). Specifically I am trying to use the PIL library, but I am assuming my problem is not limited to PIL. I have the PIL library installed in my regular python and in anaconda and can access it in python scripts but cannot access it from RhinoPython. I do not have IronPython installed separately, other than via Rhino, so I cannot install PIL via something like:
ipy -X:Frames -m pip install PIL
from the command-line because “ipy” is not a recognised command and there isn’t an “ipy” executable inside the C:\Program Files\Rhino 6\Plug-ins\IronPython folder.

The python code I would like to run is along the lines of:
from PIL import Image
my_image = Image.open(my_image_path, ‘r’)
However, I get the runtime error message “No module named PIL” in the EditPythonScript debug console.

I have downloaded the python 2.7 version of PIL from here:
https://www.pythonware.com/products/pil/
and extracted the PIL folder to C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\site-packages
I have added the folder to the ModuleSearchPaths list in the EditPythonScript Options dialog box.

Can anyone give a quick overview of how to add external python packages to Rhino’s IronPython subsystem?

Hello Paul,

it’s a bit tricky in Rhino because Rhino uses Ironpython, version 2.7, not CPython. This means you are pretty much limited to pure python modules circa 2015, not anything with compiled code (which probably includes PIL / pillow). Search this forum for PIL and pillow and you will find dotNet alternatives for image processing.

You can use subprocess with an external python interpreter or use GHPython remote (I havent tried that as i dont really use GrassHopper)

-Graham

1 Like

For what it’s worth, by adding the folder in the ways you described:

it seems to load in Rhino 6. Granted, I have no idea if it will run, but it loaded.
You may have to click on the “Reset Script Engine” once you add the folder path.
PIL

1 Like

Chris,

Thanks for trying this out. But when I do what you have in your screenshot I don’t get the autocomplete popup and it complains that there is no Module named PIL when I run the code.
(I am using Rhino6)

Paul.

try running the script with just the import statement and see if it loads? I didn’t do anything different than what you had tried…

Thanks Graham.

PIL doesn’t seem to be a Compiled library, since all the files in the PIL folder are .py files.

I don’t think I can subprocess the work to an external python interpreter since, whilst it would be able to run the PIL related code, I guess it wouldn’t be able to generate rhino geometry as part of the sub-process?

I tried just the import line and it gave the module missing error.
Not sure how you have got it to work! Do you happen to have ironpython installed separate from Rhino?
Which version of PIL did you download and how did you put it into the site-packages folder?

I do have a seperate IronPython install (2.7.3) that i am 99% sure gets installed with Revit/dynamo deployments. Technically, I also have Python 2.7.10 that gets installed with ArcGIS. I guess it’s possible that has something to do with it.
I downloaded PIL-1.1.7.win32-py2.7.exe from the link you posted, ran the installer, (it found the PYTHON install for ArcGIS), it extracted the files there.
I just moved the folder from there to the IronPython Plug-ins folder.

Chris - Inspired by your success I have managed to recreate what you did.
I downloaded and extracted the tar.gz version of PIL from here:
http://effbot.org/downloads#pil
rather than simply the PIL folder full of .py files I had got from the .exe version of the library on the https://www.pythonware.com/products/pil/ page.

With the full tarball version it did import the PIL library and had auto-complete pop-ups just like you got.

However, whilst some of the PIL commands (like Image.open) now work fine, some of them (like Image.getdata) do not - RhinpPython gives the error “The _imaging C module is not installed”.

I guess this is now suffering from what Graham suggested would happen. The PIL library (from the tarball) does indeed have precompiled code. And RhinoPython can’t handle it.

1 Like

Yeah…I think @Dancergraham hit it on the head! I don’t think this one will run very well, if at all.

When I ran the exe installer it said it couldn’t install it because it couldn’t find any installation of python 2.7, so I had to manually extract it from the exe using 7zip.
I think your existing ironpython install helped you with this. but I have managed to recreate your success using the full tar.gz version of PIL from the web, rather than the exe version.

I’d be interested to know whether the folder you moved contained only .py files, or did it have a lot of sub-folders and .c files too?

246 files total
py, pyc and pyo files, no sub folders.
here’s the folder I have.
PIL.zip (986.7 KB)

Thanks for sharing. You got my hopes up there for a second, but I tried your ZIP and it worked, but still gave the “_imaging C module is not installed” error message when I tried Image.getdata.
I think I’m going to give in and accept PIL isn’t going to work, as Graham said from the start.

I’ll open the bitmaps using a binary file read, parse the header and extract the image data myself.

No , though an alternative would be to use the Rhino3dm library externally if you can cope with the limited capabilities

1 Like

Depending on what exactly you’re implemented PIL for, you might consider switching it out for a .NET library instead (i.e. that will work with IronPython). The standard System.Drawing namespace might meet your requirements:

1 Like

@AndersDeleuran - Thanks very much for the suggestion. I managed to do what I wanted within 10mins using the System.Drawing.Bitmap class. Much less of a personal challenge than reading the bitmap bytewise myself, but it has given me my weekend back so thanks for that!

1 Like

@Dancergraham I didn’t know about being able to use Rhino3dm externally. I don’t need the computational overhead of having Rhino running in the background, so I might use Rhino3dm to run everything externally (and get access to PIL in the process), create my geometry and save it in a Rhino file for later access. Thanks for pointing it out.

1 Like

Ver’ welcome.

Be aware of the limitations- it seems fine for handling simple geometry but lacks functionality like lofts, for which you need rhino or rhinocompute. OK for generating regular grids, not suitable for , say, creating a full 3D badger :wink:

-g