PIL: Python Image Library (Maya) - error loading _imaging module

I want to test some basic image processing from Maya via Python.
I’ve installed Python 2.7 and the PIL-package - and then copied the PIL folder to
X:\Autodesk\Maya2014\Python\Lib\site-packages\

Now I’m trying to multiply each pixel in an image by 1.2 using the below code:


from PIL import Image

im = Image.open(r"X:	he\path	est.tga")
im.point(lambda i: i * 1.2)
im.save(r"X:	he\path	est.tga")

However, this raises an error:

Error: The _imaging C module is not installed

…so I added another import and then tries to run the script:
from PIL import _imaging

…but then I get the error: # ImportError: DLL load failed: %1 is not a valid Win32 application.

Also, just doing a global import doesn’t fix it:
import PIL

Checked the documentation:
http://effbot.org/zone/pil-imaging-not-installed.htm

…and when just running import _imaging I get

ImportError: No module named _imaging

…which suggests that maybe “the problem might be that the module found by Python isn’t compatible with the Python interpreter”
And that would suck because as most of us know, Autodesk use their own versions of Python in Maya -_-

What to do?

Short answer:
It is because with Maya 2013 and on, PIL (any other extensions) needs to be compiled against VS2010.

There is another thread on this topic floating around with more details, i’ll try to dig it up.

Edit: here is the thread with more info

I’ll be damned. That’s just plain idiotic comming from Autodesk.
Ok, that does explain the problem - but it doesn’t help me because I have no idea how to compile, decompile or recompile stuff.

ive been trying for the past hour or so, but realized trying to compile with 2012 isnt going to work :frowning: And my 2010 express doesnt have a 64bit compiler so im SOL until i can get a real 2010 version.

Sorry

In my scripts that use PIL, I usually use some standalone modules executed with os.system. I want these modules to work whether or not the user has Maya, but I know that’s not an option in all cases. If you’re just playing with PIL, though, that’s a good way to get started.

So is this a valid method? I was able to import PIL, Image and _imaging in Maya doing this, but I haven’t played with it much. I’m on OSX.

In a python interpreter,


>>> import Image
>>> Image.__file__
'/usr/local/lib/python2.7/site-packages/PIL/Image.pyc'

Now I know PIL’s path on my system. In Maya, I add the folder to the sys.path.


import sys
sys.path.append('/usr/local/lib/python2.7/site-packages/')
from PIL import Image
# or 
sys.path.append('/usr/local/lib/python2.7/site-packages/PIL/')
import Image
import _imaging

That seems to be working for me so far, maybe it’s a bit of hack, though.

[QUOTE=TheMaxx;23443]ive been trying for the past hour or so, but realized trying to compile with 2012 isnt going to work :frowning: And my 2010 express doesnt have a 64bit compiler so im SOL until i can get a real 2010 version.

Sorry[/QUOTE]

You should be able to compile it with the Windows 7 SDK. Although some components which use VS projects may be tricky to compile because you have to do it from the command line with msbuild. Definitely try to build it first without any addons like littleCMS, Freetype and whatever else. I think that shouldn’t be too difficult. zlib should also be fairly straight forward to build with the Win 7 SDK only.

[QUOTE=GearSpinner;23445]…

That seems to be working for me so far, maybe it’s a bit of hack, though.[/QUOTE]

There must be some other reason you get this working because it’s not a pathing problem.
To confirm this, I appended the path to the PIL folder like in your example, and then tried running my Python code that multiplies the pixels.
Result: same error as before.

So somehow you have an _imaging module that is compatible with Maya 2014 :confused:

EDIT: On a side note: Does this issue mean that if one is to develop a public tool using PIL, you have to include the following PIL “versions” in the download for said tool?:
PIL 1.1.7 for Python 2.7 - compiled in VS2010 (x86) - For Maya 2014 users using the x86 version of the app
PIL 1.1.7 for Python 2.7 - compiled in VS2010 (x64) - For Maya 2014 users using the x64 version of the app
PIL 1.1.7 for Python 2.6 - For Maya 2013 users and below

…or am I mistaken?

EDIT2: It seems that another TA (Christian Åkesson) has already compiled PIL for Maya 2014 x64. Check this out:
http://christianakesson.com/2013/4615
No more error messages, but the code I wrote in the OP doesn’t seem to work. No multiplication is done on the pixels! Tried pretty much everything on http://effbot.org/imagingbook/introduction.htm so either the modules are not working or my code is wrong.

[QUOTE=Nightshade;23448]
EDIT: On a side note: Does this issue mean that if one is to develop a public tool using PIL, you have to include the following PIL “versions” in the download for said tool?:
PIL 1.1.7 for Python 2.7 - compiled in VS2010 (x86) - For Maya 2014 users using the x86 version of the app
PIL 1.1.7 for Python 2.7 - compiled in VS2010 (x64) - For Maya 2014 users using the x64 version of the app
PIL 1.1.7 for Python 2.6 - For Maya 2013 users and below[/QUOTE]

Yes it does, but I think you can compile your tool and it’s extensions into a single .exe… I dunno I have never tried that…

I have used py2exe to get a single exe. Took bit of digging around to get it to work but it does :smiley:

A follow up question on this matter:
Say that I want this tool to be -for- Maya (ie: it loads up in Maya, and uses Maya commands to build a UI for said tool), would it then be easier to just take all those PIL Python files - and the rest of the stuff my tool uses - and make a Maya plugin for it?
I’ve never done any compiling of any sort, but eventually want to learn how to compile plugins for Maya that the user then can just install via some say, open-source installer or something. (I like user-friendlyness!)

[QUOTE=RobertKist;23446]You should be able to compile it with the Windows 7 SDK. Although some components which use VS projects may be tricky to compile because you have to do it from the command line with msbuild. Definitely try to build it first without any addons like littleCMS, Freetype and whatever else. I think that shouldn’t be too difficult. zlib should also be fairly straight forward to build with the Win 7 SDK only.[/QUOTE]I tried, but i think my 2010 installed is boned as the sdk wont install. I’ll see if i can get a full version and build some of these things, im sure this wont be the last.The lfd site is nice until it comes to the maya bins, then youre boned

I got it working for the most part. I was able to read/write jpeg and png. TIFFs failed for me however. If all you need is jpg and png:

https://drive.google.com/file/d/0B2MGMFzV11wXa2xMdVJSbC0yeG8

I’ll keep cracking on the tiff stuff if its needed, otherwise, use with caution.

This was compiled for Maya 2014 btw