Python PIL has no attribute 'Image' - python

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

Related

trouble importing precompiled version of PIL on linux

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.

imageio: AttributeError: 'module' object has no attribute 'imread'

I come up with the error AttributeError: 'module' object has no attribute 'imread' while using the imageio module with the following code. I searched the community a lot but all of the errors about imread command are discussed under scipy, but nothing for imageio. So, any comments or links are appreciated! Here is my code:
import os
import imageio
os.chdir('C:/concent')
png_dir='gif/'
images=[]
for file_name in os.listdir(png_dir):
if file_name.endswith('.png'):
file_path = os.path.join(png_dir, file_name)
images.append(imageio.imread(file_path))
imageio.mimsave('gif/movie.gif', images)
Probably you have another file named imageio.py in the working directory.
The import statement will import files from the current directory before searching standard library and site package locations.
You should be able to fix the problem by renaming your local imageio.py file to something else that does not clash with other modules.

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.

Python Import error Opencv NameError: name highgui is not defined

Python 2.7.5
I added the homebrew/science to my brew taps.
I ran
brew install opencv.
bash profile I added
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
I've opened the headgazer folder and run
python tracker.py
Traceback (most recent call last):
File "tracker.py", line 21, in <module>
from roi_detector import ViolaJonesRoi
File "/Users/username/Downloads/headtracker_version_0.0/roi_detector.py", line 21, in <module>
import opencv as cv
ImportError: No module named opencv
~/Downloads/headtracker_version_0.0:.
Ok, looks like it's called opencv2. So I swap out occurances of import opencv as cv with
import cv2 as cv
now in viola_jones_opencv.py I have
import cv2 as cv
from cv import *
from cv.highgui import *
And I get an error on importing highgui
ImportError: No module named highgui
there is no highgui module in opencv's python api. (full-stop)
actually , all your import statements look dorky.
(renaming cv2 to cv is a bad idea, since there existed an old cv module before. you're only confusing yourself and others this way)
replace all of them with :
import cv2
and stick with :
cv2.imshow()
cv2.waitKey()
etc
[EDIT]
if you're trying to run something like this ,
then there's bad news for you. opencv comes with it's own python bindings since a very long time now, but apart from that, there exist several outdated 3rd party bindings. the code you're trying to run seems to be one of those, so you can't use it with opencv's builtin api.
there is no highgui module so I do not know what you ar doing. Also, I agree with berak as renaming anything imported is a pretty bad idea. You just sometimes dont know if there is another directory named the same thing. Good luck on your fix anyways.

AttributeError: 'module' object (scipy) has no attribute 'misc'

I updated from ubuntu 12.04 to ubuntu 12.10 and the python module I have written suddenly no longer works with the error message that the module scipy does not have the attribute 'misc'. This worked previously. I am still using python 2.7 after the update. Here is where the code crashes
import scipy
scipy.misc.imsave(slice,dat)
Any ideas?
>>> import scipy
>>> scipy.misc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'misc'
>>>
>>>
>>> import scipy.misc
>>> scipy.misc.imsave
<function imsave at 0x19cfa28>
>>>
Which seems to be quite common with scipy.
Because you cannot directly use the misc module from scipy without explicitly import it. Here is the way of loading scipy.misc:
import scipy.misc
#Load the Lena image into an array, (yes scipy does have a lena function)
lena = scipy.misc.lena()
...
imread is depreciated after version 1.2.0 !
So to solve the problem I had to install 1.1.0 version.
pip install scipy==1.1.0
You need to explicitly import scipy.misc as:
import scipy.misc
You need to install the package pillow (formerly known as PIL), if not already installed. For image manipulation functions of scipy.misc such as imread() or imsave() to function correctly, pillow has to be installed.
To verify, either run your code again or type the below command:
scipy.misc.imread
I had a similar problem. In my case, I was trying to import comb from scipy.misc, which had been depreciated in scipy 1.0.0 (see ref here). Thus, I was inevitabely getting AttributeError: module 'scipy.misc' has no attribute 'comb'.
Replacing scipy.misc.comb with scipy.special.comb fixed the issue.

Categories

Resources