21

I get the error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-0f6709e38f49> in <module>()
----> 1 from PIL import Image

C:\Anaconda\lib\site-packages\PIL\Image.py in <module>()
     61     from PIL import _imaging as core
     62     if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
---> 63         raise ImportError("The _imaging extension was built for another "
     64                           " version of Pillow or PIL")
     65 

ImportError: The _imaging extension was built for another  version of Pillow or PIL

Whenever I try to use the PIL library. I'm trying to load and work on a bunch of .gif's, and what I'm trying now, is the following:

from PIL import Image

Trying a different approach, through scipy with:

import scipy.ndimage as spnd
os.chdir('C:\\WeatherSink\\data\\')
spnd.imread('2014-11-03-0645.gif')

Fails with:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-23c383b79646> in <module>()
      1 os.chdir('C:\\WeatherSink\\data\\')
----> 2 spnd.imread('2014-11-03-0645.gif')

C:\Anaconda\lib\site-packages\scipy\ndimage\io.pyc in imread(fname, flatten, mode)
     36         from PIL import Image
     37     except ImportError:
---> 38         raise ImportError("Could not import the Python Imaging Library (PIL)"
     39                           " required to load image files.  Please refer to"
     40                           " http://pypi.python.org/pypi/PIL/ for installation"

ImportError: Could not import the Python Imaging Library (PIL) required to load image files.  Please refer to http://pypi.python.org/pypi/PIL/ for installation instructions.

The first approach guides me towards the versions of PIL installed. I try emulating the getattr(...), and that returns None. So I'm not surprised that it's less than functioning. But does anyone know how to 'fix' the errors?

I'm running on win7, managing python2.7 through conda. I've tried to remove and re-install the packages as well, without any change in the output.

Help is much appreciated.

C.Buhl
  • 311
  • 1
  • 2
  • 9
  • 2
    First guess is you have more than one version of PIL or Pillow installed, and possibly with different environments. Find them all, uninstall them all, then reinstall a fresh Pillow. – Hugo Nov 04 '14 at 10:14

6 Answers6

21

This is only an installation issue.

First install pip on your system if it is not installed. It is available for Windows also.

Upgrade your numpy, pip/pillow, scipy:

pip install -U numpy
pip install -U pil/pillow
pip install -U scipy

The best option for Windows is to use anaconda.

I think pip is already installed in conda. This will resolve your system version issue.

In [1]: from PIL import Image

In [2]: import scipy.ndimage as spnd

In [3]: x = spnd.imread('ppuf100X91.gif')

In [4]: print x
[[255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]
 ..., 
 [255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]]
anothernode
  • 5,100
  • 13
  • 43
  • 62
aibotnet
  • 1,326
  • 1
  • 13
  • 27
  • 4
    It solved my problem, thx! I had to use `pip install -U pillow` `(`pip install -U pil` gave `Could not find a version that satisfies the requirement pil`). – Piotr Migdal Jan 22 '16 at 16:16
9

This is a problem in python 3.6 Edit file: C:\Anaconda\lib\site-packages\PIL\Image.py and change code:

if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
     raise ImportError("The _imaging extension was built for another "
                        " version of Pillow or PIL")

change that to:

if core.PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
     raise ImportError("The _imaging extension was built for another "
                        " version of Pillow or PIL")

This will solve the problem. Regards

0x90
  • 39,472
  • 36
  • 165
  • 245
ibrahim
  • 99
  • 1
  • 2
  • 1
    Hi Ibrahim, does this also apply to Python 2.7? And could you emphasize what the difference is between the two codebits? – C.Buhl May 05 '17 at 13:16
  • 1
    Yes, in python 2.7, it has the same problem. The problem is it imports all symbols with name space core and address the constant without namespace. – ibrahim May 07 '17 at 02:01
  • 2
    Please ***do not do this***. That check is to ensure Pillow can trust its core is compatible with its public API. If you do this, you may see all sorts of weird bugs as one layer becomes out of sync with the other. Instead, make sure you *only have one version of Pillow installed*, eg. uninstall the conda version and install from pip. – Hugo May 17 '18 at 17:23
  • Thank you Hugo ! :-) This is the right answer - uninstall pillow using Conda and reinstall it using PIP. – ProfVersaggi Jan 27 '19 at 15:18
3

Maybe one of your dependencies requires PIL and PIL ends up being installed after Pillow, causing conflicts in your site packages dir. I'm assuming you're seeing that error because the import statement is importing _imaging from a legitimate PIL installation and not a Pillow installation.

I've had trouble in the past with conflicting packages that require either PIL or Pillow. Pillow is, of course, the preferred package. I would take a look at the dependencies of your packages. If you can find one that depends on PIL, I would submit a pull request which changes the dependency to Pillow or maybe even create your own fork with that change. For my situation, forking was the option that I settled on since the project seemed to have had no activity on it for a long time.

Ultimately, you want to eliminate any dependencies on the PIL package (since it's no longer active) in favor of Pillow.

David Sanders
  • 4,069
  • 1
  • 24
  • 38
3

This problem is because of Python package of PIL/pillow is Up or Down version of your system and due to this question is generate in your system.

Try to check this command:

sudo apt-get install python-PIL

Check this package is install or not. If it is installed than try to remove with the command:

sudo apt-get remove python-PIL

Check this will work to remove the PIL/pillow package is removed from your system.

And finally this command will help you to solve this package problem:

sudo apt-get autoremove python-PIL

Then reinstall PIL/Pillow package:

sudo apt-get install python-pil

This should help you solve the problem.

0

i think acttually is the Virtual Environments you use when you run your code. many times computer will run Anaconda path instead of Python3. So you can try python or python 3 in command prompt before call your code. Example: python3 image.py. Or you can delete anaconda :D.

0

Reinstall the package which the error mentioned maybe also can help you. It works on my computer when pip install -U don't work.

Faheel
  • 2,435
  • 24
  • 33
Hobo Fly
  • 11
  • 2