trouble importing precompiled version of PIL on linux - python

I am writing a plugin for Ultimaker Cura which uses the Python Imaging Library.
Cura has it's own python environment and uses Python 3.5.7.
I want my plugin to be usable for any Cura user, so I have to include PIL in my plugin inside a subdirectory, and because it's _imaging module is written in C, I have to include precompiled versions of PIL for Python 3.5 which i got from here: https://pypi.org/project/Pillow/#files
I included the cp35 win_amd64 version for windows under "lib_win" and the cp35 manylinux1_x86_64 version for linux under "lib_linux". Here is my code:
if sys.platform == 'win32':
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib_win"))
from PIL import Image
from PIL import ImageFilter
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageChops
if sys.platform == 'linux':
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib_linux"))
from PIL import Image
from PIL import ImageFilter
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageChops
This works without any issues for windows. Under linux, i get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dipl/.local/share/cura/4.6/plugins/SVGReader/lib_linux/PIL/Image.py", line 93, in <module>
from . import _imaging as core
ImportError: cannot import name '_imaging'
I have "_imaging.cpython-35m-i386-linux-gnu.so" inside the lib_linux/PIL directory, but it does not recognize this.
I have checked other posts with the same error, but they either have older versions of PIL installed, or use the wrong precompiled version, or are missing a DLL. None of this is the case here.
I also have a working version of PIL on my linux system, and the include code and the PIL code look exactly the same, only the python version number is different.
In case you need it, here is the full sys.path for the Cura python environment (my plugin is SVGReader): https://i.stack.imgur.com/Mixzi.png
(Had to use my own console because Cura doesn't come with one)
So, why does it not recognize _imaging? Any ideas?

in case anybody has the same issue, it's really just as simple as changing the filename of the _imaging library. Name it _imaging.so, or create a symlink to it called _imaging.so. No idea why it works on other platforms without doing that. Might need to rename some other stuff in the same manner, too.

Related

Python import Pillow/PIL error, cannot ImportError: cannot import name '_imaging'

I've tried everything, adding the files to my actual project, installing and reinstalling PILLOW. Right now the actual PIL file is in my project folder and I currently have the error:
File "C:\Users\crowl\Desktop\py project\PIL\Image.py", line 69, in
from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL' (C:\Users\crowl\Desktop\py project\PIL__init__.py)
I've tried to import Image, from PIL import *, from PIL.Image import core as _imaging and currently using the from PIL import Image. I managed to get it to work on another PC by just using the PIL folder with all the source code as it's a school project and now I'm at home and it doesn't seem to work.
Appreciate all the help.

Why does "import PIL; PIL.Image" not work, but "from PIL import Image" does?

In a python interpreter:
>>> import PIL
>>> PIL.Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Image'
>>> from PIL import Image
>>> PIL.Image
<module 'PIL.Image' from '/usr/lib/python2.7/site-packages/PIL/Image.pyc'>
Why do I have to make the import as "from PIL import Image"? I'm interested in both "what is the underlying working in python imports that makes this behaviour possible?" and "Why was the PIL package designed to work like this?"
Also, I really like to keep a clean namespace when programming. If I want to use PIL.Image in my code, should I import like this:
>>> import PIL
>>> from PIL import Image
>>> del Image
or is there a better way?
You could import PIL.Image:
import PIL.Image
PIL.Image.open('my_pic.jpg')
I think that Pillow is structured this way because of the history of the package. The original package PIL allowed you to do import Image. Pillow, the fork of PIL which supports Python 3, moved Image to the PIL namespace. The suggested import from PIL import Image makes it easy to switch from PIL to Pillow. See the porting docs for more info.
PIL.Image is a submodule of PIL and so won't be automatically imported with,
import PIL
since Python doesn't recursively import submodules.
5.4.2. Submodules in the Python Language Reference may help to understand the behaviour of importing submodules.
When a submodule is loaded using any mechanism, ... a binding is placed in the parent module’s namespace to the submodule object.
So although after importing and loading a submodule,
import PIL
from PIL import Image
you are able to access it via PIL.Image, this does not mean PIL.Image is loaded when importing the PIL module.
Also, I couldn't find this explicitly stated anywhere but from what I've tested, it seems to be that when you import a submodule either like:
import package.submodule
or:
from package import submodule
The parent package is also loaded in the process.

Wand + ImageMagick + Anaconda: "'wand' has no attribute 'image'"

I'm having issues using these three together. I believe wand is not recognizing the ImageMagick libraries but I'm not sure.
Environment:
Python 3.5.1 :: Anaconda 4.0.0 (64-bit)
Windows 7
Set up instructions I took:
Installed ImageMagick-6.9.4-Q8 (x64) with the "C/C++ development
headers options checked. (Installed to C:\Program
Files\ImageMagick-6.9.4-Q8)
Set MAGICK_HOME envar C:\Program Files\ImageMagick-6.9.4-Q8
Installed wand from pip
My code:
import wand
...
with wand.image.Image(filename=source_file, resolution=(RESOLUTION, RESOLUTION)) as img:
...
Traceback:
Traceback (most recent call last):
File ".\pdf_convert.py", line 31, in <module>
ret = pdf2jpg(f, target_file, 2480)
File ".\pdf_convert.py", line 10, in pdf2jpg
with wand.image.Image(filename=source_file, resolution=(RESOLUTION, RESOLUTION)) as img:
AttributeError: module 'wand' has no attribute 'image'
From everything I've seen I've followed the right setup instructions. I am using the 64 bit version of ImageMagick with the 64 bit version of Anaconda. This was working with me before until I started using Anaconda (before I was using regular 32 bit Python and 32 bit ImageMagick.)
Is there something I'm missing? Why is wand not working correctly?
Try this
from wand.image import Image
with Image(filename=source_file, resolution=(RESOLUTION, RESOLUTION)) as img:
pass
Is there something I'm missing? Why is wand not working correctly?
I believe it is working as expected, and the original architect did not intend to allow top-package-level shortcuts (i.e. import wand). This kinda makes sense as wand integrates to IM with ctypes, and does not attempt to resolve libraries during setup.py.
You can modify the package to include the module shortcuts your expecting by adding the following.
# wand/__init__.py
import api
import color
import compat
import display
import drawing
import exceptions
import font
import image
import resource
import sequence
import version
But I wouldn't recommend this. The from package.module import Class is a lot more cleaner.
If you are using PIL.Image as well then use:
from wand.image import Image as wand_image_Image
import PIL

Import pillow without installing

I am working on a Python project that requires PIL to show images. However, the computers that I am working on often do not allow me to install things, and have a very bare bones python setup. For this reason, most of the modules that I need I simply place in the same directory as my python files.
I tried doing the same with PIL. I downloaded the pillow source, and copied the PIL folder into my project. I was then able to run "import PIL" with no problems. However, when I then tried to run "from PIL import Image" I get the error: "The _Imaging C module is not installed". From other searches I think that installing Pillow properly would fix this problem, however I would like PIL to be more portable, and not require an instillation.
Any ideas would be great. Thanks in advance.
One solution is bundle PIL in with the script in .egg form. Then, you can import PIL directly from the .egg instead of having to install it:
How to create Python egg file
The basic process is as follows:
How to create egg:
Edit PIL's setup.py to include from setuptools import setup instead of normal setup import
Run python setup.py bdist_egg
Egg will be inside of dist/
How to import egg:
Copy .egg file to script's directory and import desired modules:
import os
import sys
DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(DIR, "./path/to/PIL.egg"))
#You can now import from PIL normally:
from PIL import Image

Python PIL has no attribute 'Image'

I'm using python2.6 and got a problem this morning. It said 'module' has no attribute 'Image'. Here is my input. Why the first time I can not use PIL.Image?
>>> import PIL
>>> PIL.Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Image'
>>> from PIL import Image
>>> Image
<module 'PIL.Image' from '/usr/lib/python2.6/dist-packages/PIL/Image.pyc'>
>>> PIL.Image
<module 'PIL.Image' from '/usr/lib/python2.6/dist-packages/PIL/Image.pyc'>
PIL's __init__.py is just an empty stub as is common. It won't magically import anything by itself.
When you do from PIL import Image it looks in the PIL package and finds the file Image.py and imports that. When you do PIL.Image you are actually doing an attribute lookup on the PIL module (which is just an empty stub unless you explicitly import stuff).
In fact, importing a module usually doesn't import submodules. os.path is a famous exception, since the os module is magic.
More info:
The Image Module
If you, like me, found the accepted answer a bit befuddling because you could swear you've been able to use
import PIL
PIL.Image
sometimes before, a potential reason for this is if any other code in your Python session has run from PIL import Image or import PIL.Image, even if it's in a completely different scope, you will be able to access PIL.Image.
In particular, matplotlib does so when it's imported. So if you run
import matplotlib
import PIL
PIL.Image
it works. Thanks, Python.
Don't trust anyone. Don't trust Python. Use import PIL.Image.
You can do:
try:
import Image
except ImportError:
from PIL import Image
it's better to use pillow instead PIL.
The solution is
instead of using this form
import PIL
PIL.Image
use this form
from PIL import Image
Image
in the Linux system both forms work fine
but in windows, there is a problem

Categories

Resources