PIL doesn't recognize jpeg library under Mac OS - python

Scripts like that:
from PIL import Image, ImageOps
img = Image.open('1.JPG')
thumb = ImageOps.fit(img, (200,200) , Image.ANTIALIAS, (0.5, 0.5))
cause this IOError:
Traceback (most recent call last):
(...)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available
How do I get jpeg support for my PIL? That issue seems to be well known but the existing threads don't solve it for me. I use brew and pip for my python packages and have already tried the following:
brew install jpeg
causes Error: jpeg-8d already installed
brew install libjpeg causes Error: jpeg-8d already installed
sudo pip install Pillow - installation works but does not change anything
How do I get jpeg support for my PIL installation? Any ideas?

I don't understand why, but reinstalling PIL fixed the issue:
sudo pip uninstall pil
sudo pip install pil

I ran into a similar problem on Ubuntu 12.04 64-bit and solved it by symlinking the libraries PIL was looking for into /usr/lib (where it was actually looking for them):
# symlink image libraries so PIL can find them
ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib
# etc for any other libraries

For non OSX people landing here, I found linking to the 64 bit libraries on my 64 bit Ubuntu system to fix the compile issue.

Related

python cannot import opencv because it can't find libjpeg.8.dylib

Trying to get opencv for python working on Mac OSX - Mavericks but keep getting an image not found for libjpeg.8.dylib when doing import cv from python
(Recently updated from Mountain Lion)
This is what I did:
1.brew tap homebrew/science
2.brew install opencv
3.python
4.import cv
-got the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: dlopen(/usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /usr/local/Cellar/opencv/2.4.9/lib/libopencv_highgui.2.4.dylib
Reason: image not found
I looked for libjpeg.8.dylib and found it in /usr/local/Cellar/jpeg/8d/lib/ which, apparently, is not where libopencv_highgui.2.4.dylib is looking.
I'm a bit new to Mac OS and homebrew. Could anyone explain how to resolve this error and get opencv running? I have the python that comes preinstalled with Mac and them python installed by homebrew.
Thank you.
The quick and dirty solution for this is to make a symlink inside of the /usr/local/lib folder pointing to the actual location of libjpeg.8.dylib, like this:
$ sudo ln -s /usr/local/Cellar/jpeg/8d/lib/libjpeg.8.dylib /usr/local/lib/libjpeg.8.dylib
The problem is opencv and python expect libjpeg.8.dylib to be in /usr/local/lib/ but homebrew installs it in /usr/local/Cellar/, hence annoying error.
I used brew a for installing OpenCV on my mac, and ran into this same problem, but it was with an Xcode C++ console application.
However an alternative method to solve the problem is to copy cp the file to the library location.
cp /usr/local/Cellar/jpeg/8d/lib/libjpeg.8.dylib /usr/local/lib/libjpeg.8.dylib
Or the latter, which I don't suggest, just stating another method is to use move mv the entire file location into your /usr/local/lib/ directory.
mv /usr/local/Cellar/jpeg/8d/lib/libjpeg.8.dylib /usr/local/lib/
Just had a similar problem to this (in python import opencv was working for me one day, then the next it threw the same error you are reporting) and this solution just worked for me:
Upgrade your homebrew opencv eg.:
brew upgrade opencv (or in my case brew upgrade opencv3)
Hope this helps
I had a similar problem with the Pillow library on macOS. The solution proposed here to install it from source worked for me. I had to install it to /usr/local/ for Pillow to find it. You'll need command-line tools, which is the purpose of the first line:
xcode-select --install
curl -O -J -L http://www.ijg.org/files/jpegsrc.v8.tar.gz
tar xvfz jpeg*tar.gz # Unzip and untar what you downloaded
cd jpeg-8 # Change directory to wherever it unpacked to
./configure --prefix="/usr/local" # Configure with the necessary prefix
make
sudo make install

Challenge installing psd-tools on fresh Mac

I've been using PSD-TOOLS to great effect on a 13" notebook for half a year now (Python 2.7.3). Just tried to do a fresh install on a Mac running 10.8.4.
I ran through the python setup at http://docs.python-guide.org/en/latest/starting/install/osx/ (Python version
Installed Pil using:
sudo pip install pil
All my Pil scripts work fine.
pip list
shows PIL (1.1.7) installed fine.
Then tried to install psd-tools using:
sudo pip install psd-tools
Appears to install fine but when I run my PSD scripts, get the following:
File "/Library/Python/2.7/site-packages/psd_tools/user_api/psd_image.py", line 91, in as_PIL
return self._psd._layer_as_PIL(self._index)
File "/Library/Python/2.7/site-packages/psd_tools/user_api/psd_image.py", line 243, in _layer_as_PIL
return pil_support.extract_layer_image(self.decoded_data, index)
File "/Library/Python/2.7/site-packages/psd_tools/user_api/pil_support.py", line 32, in extract_layer_image
decoded_data.header.depth, get_icc_profile(decoded_data))
File "/Library/Python/2.7/site-packages/psd_tools/user_api/pil_support.py", line 68, in _channels_data_to_PIL
raise Exception("This module requires PIL (or Pillow) installed.")
Exception: This module requires PIL (or Pillow) installed.
Anybody run into this issue?
I ran into this issue on OSX 10.9 with Python 2.7.5. (In a virtualenv)
I ended up going over https://pypi.python.org/pypi/Pillow/2.2.1 and installing some optional prerequisites for Pillow.
#I already had libjpeg but this is the line in their doc.
brew install libtiff libjpeg webp littlecms
#Then re-installed Pillow
pip install --force-reinstall --upgrade pillow
The result was the following features enabled
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- TIFF G3/G4 (experimental) support available
*** FREETYPE2 support not available
--- LITTLECMS support available
--- WEBP support available
--- WEBPMUX support available
And my small python test ran
from PIL import Image
from psd_tools import PSDImage
psd = PSDImage.load('test_data/1.psd')
merged_image = psd.as_PIL()
Not sure which of those requirements was responsible so if you are not interested in installing extra libraries you may have to do 1 at a time or dig into the source.

No module named Image

Sorry for my grammar, I don't speak English.
After I set filebrowser, tinymce, and grappelli, I get this error: No module named Image
try:
from PIL import Image
except ImportError:
import Image
I set it to PIL but it didn't solve the problem.
my platform windows
If i want: pip install PIL
`c:\Users\Kim\BitNami DjangoStack projects\homex8>pip install PIL
Downloading/unpacking PIL
Running setup.py egg_info for package PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
Installing collected packages: PIL
Running setup.py install for PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
building '_imaging' extension
error: Unable to find vcvarsall.bat`
I do not understand what that means
Solved problem.
reinstall PIL with easy_install, and more movements, here are the details.
You are missing PIL (Python Image Library and Imaging package). To install PIL I used
pip install pillow
For my machine running Mac OSX 10.6.8, I downloaded Imaging package and installed it from source.
http://effbot.org/downloads/Imaging-1.1.6.tar.gz and cd into Download directory. Then run these:
$ gunzip Imaging-1.1.6.tar.gz
$ tar xvf Imaging-1.1.6.tar
$ cd Imaging-1.1.6
$ python setup.py install
Or if you have PIP installed in your Mac
pip install http://effbot.org/downloads/Imaging-1.1.6.tar.gz
then you can use:
from PIL import Image
in your python code.
Did you setup PIL module? Link
You can try to reinstall it on your computer.
It is changed to : from PIL.Image import core as image
for new versions.
You can this query:
pip install image
I had pillow installed, and still, I got the error that you mentioned. But after I executed the above command, the error vanished. And My program worked perfectly.
Problem:
~$ simple-image-reducer
Traceback (most recent call last):
File "/usr/bin/simple-image-reducer", line 28, in <module>
import Image
**ImportError: No module named Image**
Reason:
Image != image
Solution:
1) make sure it is available
python -m pip install Image
2) where is it available?
sudo find ~ -name image -type d
-->> directory /home/MyHomeDir/.local/lib/python2.7/site-packages/image
->> OK
3) make simple-image-reducer understand via link:
ln -s ~/.local/lib/python2.7/site-packages/image
~/.local/lib/python2.7/site-packages/Image
4)
invoke simple-image-reducer again.
Works:-)
If you are trying to just show the image on your notebook, below simply does the job
from IPython.display import Image
Image(url= "Link to the image ending with file format such as, .jpg or .png")

How to install PIL in Ubuntu 11.04?

I see this question asked all over the internet, and I've tried following them all, but I still can't get PIL to work.
I tried symbolically linking the zlib, jpeg, etc. libraries via:
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/
I tried editing the setup.py file, adding this line:
add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu")
In fact, running $ sudo python setup.py install shows that JPEG, ZLIB/PNG, etc. support is Available. (I'm installing it for both 2.5 and 2.7, works in neither)
sudo python2.5 setup.py install
running install
running build
running build_py
running build_ext
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.5.6 (r256:88840, Feb 1 2012, 15:55:08)
[GCC 4.5.2]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
--- LITTLECMS support available
But after all that, I still get a decoder %s not available error.
I'm at my wits end. Anything else I might have missed?
My environment:
64-bit Ubuntu 11.04 running in a VirtualBox VM.
Here's what I do to test if PIL works or not
$ python
>>> from PIL import Image
>>> im = Image.open("photo.jpg")
>>> im.rotate(45)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1359, in rotate
self.load()
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available
>>>
As always, use the package manager:
sudo apt-get install python-imaging
It'll deal with it all for you. The packages are available.
Manually installing, in any Linux distro, is a wasted endeavour, unless the packages really don't exist. Package maintainers spend time ensuring that the package works and installs correctly, there is no point duplicating their effort. Especially not to manually install something that then doesn't have the advantages of a package - no automatic updating, no easy removal, etc...
I have successfully reinstalled PIL in Ubuntu 12.04 like this:
pip uninstall PIL
apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev
ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/
ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/
ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/
pip install -U PIL
It does not raise the IOError: decoder zip not available anymore after reinstalling the PIL. My error traceback was:
Traceback (most recent call last):
File "convert_image.py", line 15, in <module>
image.save('output.png')
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1406, in save
self.load()
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available
Try reinstalling from scratch:
Look for lib/pythonXX/site-packages/PIL. Delete all this directory along with the file PIL.pth. This should completely remove te package.
Unpack the PIL installation files from the *tar.gz you downloaded.
Add the directories where your jpeg library is, with add_directory(...) as you did before.
(Use ldconfig -P | grep jpeg to find where the libraries are).
Retry python setup.py build, then python setup.py install. Test it.
My experience was:
Not performing step 2 did not rebuild the package.
Not performing step 3 was the root cause.
Not performing step 1 may have played a part.
This seems to be a bug in PIL installation, not in Ubuntu's or any distro's package structure.
And just for the record: it is quite common to have more than one Python version installed on a system, which makes it necessary to install packages manually. Some people have a 2.x with a 3.x for experimenting, shared hostings have 2.5s and applications need a 2.7, just to give two examples.
I found it to be a combination of the two above when installing with a requirements.txt on Ubuntu. I'm using Vagrant to run a chef script, and found this approach works best for me:
First, I use a bash script to setup PIL:
#!/usr/bin/env bash
sudo apt-get build-dep python-imaging
sudo ln -s -f /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s -f /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s -f /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/
Next, during the requirements.txt install, the top of the file looks like:
--allow-external PIL
--allow-unverified PIL
Django==1.5.4
PIL==1.1.7
...
Of course, this is an unsecured way of doing it, but works for dev and quick builds. For production, it's best to download and verify all packages manually and install them from a local managed repository.

_imaging C module error in python PIL

I have read the other posts about the notorious _imaging C module error when installing PIL on Mac OS X and none of the solutions provided anywhere, including the PIL FAQ, have proven helpful.
I have the newest versions of libjpeg and zlib freshly installed from source. I have edited the Makefiles in each of these to include the option -arch i386 in the LD_FLAGS variable for 32-bit builds. PIL installs with no problems of any kind and the install summary printed to the terminal says that JPEG, TIFF, and PNG support are all OK. After that I try the self test:
new-host:Imaging-1.1.7 ely$ python selftest.py
*** The _imaging C module is not installed
This is commonly seen for a variety of reasons. Probing deeper, here I try to import _imaging directly in python.
new-host:Imaging-1.1.7 ely$ python
ActivePython 2.7.1.4 (ActiveState Software Inc.) based on
Python 2.7.1 (r271:86832, Feb 7 2011, 11:33:10)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> import _imaging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/_imaging.so
Expected in: dynamic lookup
Here again, the 'Symbol not found: _jpeg_resync_to_restart' is notorious and common, and many people have suggested this has to do with incorrect path to libjpeg. I've checked again and again and I only have libjpeg (as well as zlib, etc.) installed in my home directory, all in separate folders, from source, and all of this is marked correctly in the file setup.py.
So, PIL should be getting its jpeg support (and zlib, etc.) from the right places. All the dependencies are installed. I can see _imaging.so in my sys.path, yet I still get this _jpeg_resync_to_restart error.
Are there any ideas that don't link to alternate posts? I have spent ~7 hours reading and trying possible solutions from posts in every forum I can find.
Since you have been trying this a few times, I recommend running a few commands to clean out the old items first and start from the beginning.
I used jpeg v8c and Imaging 1.1.6 on Mac OS X, 10.6 and 10.7
get v8c of jpeg
cd into jpeg directory.
sudo make clean
CC="gcc -arch i386" ./configure --enable-shared --enable-static
make
sudo make install
get imaging i am using 1.1.6
untar
cd into imaging
sudo rm -rf build
vi setup.py
JPEG_ROOT = libinclude(“/usr/local/lib”)
sudo python setup.py install
And it’s that simple
Run your python interpreter,
import PIL
import _imaging
import Image
if all is well, then your all set.
Here is the full article on my blog Python 2.7, OSX Lion, PIL and Imaging
I have faced the same problem this evening on my mac running Mac OS X v10.7.5, Python v2.7.1 with PIL Imaging 1.1.7 and jpeg 8d.
Summarizing - the key to success in my case was:
export ARCHFLAGS="-arch x86_64"
Nothing from the other answers have solved the issue, constantly I've been getting:
The _imaging C module is not installed caused by:
Symbol not found: _jpeg_resync_to_restart.
Digging here and there finally I've found the solution, at least works for me:
ensure you don't have any obvious libjpeg libs on the system: find / -iname "libjpe*" will tell you that. I have temporarily changed places where they exist so no one could find them (especially /sw was interacting with PIL in my case):
mv opt opt-OFF
mv sw sw-OFF
These locations have been created by mac ports and fink - be warned that it is quite possible that FREETYPE2 might not be found by PIL after this move - if you need it, then just rename libjpeg parts in those locations.
in PIL Imaging src directory edit setup.py as described above to set JPEG_ROOT to /usr/local, try to run:
rm -rf build ; python setup.py build 2> /tmp/err > /tmp/log
Check in /tmp/log - you should not see JPEG support available. You can now go to the next step.
go to jpeg-8d src directory and run:
export ARCHFLAGS="-arch x86_64"
sudo make clean; CC="gcc -arch x86_64" ./configure --enable-shared --enable-static
sudo make install
go back to PIL Imaging, run these commands:
export ARCHFLAGS="-arch x86_64"
sudo rm -rf build ; python setup.py build 2> /tmp/err > /tmp/log
Check in /tmp/log - you should now see "JPEG support available", check /tmp/err - search for "jpeg" - if you see something like this:
ld: warning: ignoring file /usr/local/lib/libjpeg.dylib, file was built for unsupported file format which is not the architecture being linked (i386) - then the arch flags (both set by ARCHFLAGS and -arch) failed to trigger - investigate that case, it is crucial. If you don't see this, then you're lucky and you can invoke the installation:
export ARCHFLAGS="-arch x86_64"
sudo rm -rf build ; sudo python setup.py install
Check if your PIL works:
python selftest.py
or
echo "import _imaging" | python && echo "Works"
I hope this helps.
The problem is that the _imaging module is linked to libjpeg dynamically, not statically. The libjpeg code is not included in the _imaging module directly. This means that your platform's dynamic linker has to be able to find libjpeg in order to load and link it. My MacOS knowledge here is fuzzy, but as I recall its dynamic linker is called dyld, and its manpage may provide more information on the options you have.
Normally, the platform's dynamic linker won't be looking in your homedirectory for libraries -- but you can tell it to, for example by setting the DYLD_LIBRARY_PATH environment variable, or editing the system-wide configuration (if there is one.) Setting that environment variable usually has to be done before you start Python, though, so it may not be suitable. You may want to try to embed the runtime search path into the _imaging extension module, which is usually done by passing -rpath to the linker -- but I don't know if MacOS's linker offers that capability. Finally, you can just build libjpeg as a static library, instead of a shared one, and have the _imaging module link against that. That would avoid the whole shared library situation for libjpeg. For libjpeg, this is probably done by running its configure script with --enable-static --disable-shared.
I tried many most all of these suggestions (as well as two other suggestions on referenced blogs) on an old 10.6 Mac install. None of them worked as-is, however, reading behind the lines I was able to fix my problem. I added to the PIL setup.py in find_include_file() right before the return 1 line "print os.path.join(directory, include)". This allowed me to track down which libjpeg PIL is building against. Then I would build PIL, find the libjpeg it referenced (various copies in /sw, /opt/, /usr/local/lib, ...), and delete that libjpeg (both header and lib files).
Finally with a clean system I built and installed libjpeg source tarball that I'd downloaded myself, followed by building and installing PIL from source. This worked. As a fallback you could always disable libjpeg by removing the above files as described or else always returning zero from the described above function in setup.py.
Works well for me on Mountain Lion 10.8.2:
Step One. Removing all jpeg packages. For MacPorts:
sudo port -f uninstall jpeg or sudo port -f uninstall jpeg #version_here
We need to remove all jpeg versions!
Step Two. Remove PIL: pip uninstall PIL
Step Three. Install jpeg package again. For MacPorts: sudo port install jpeg
Step Four. Install PIL again: pip install PIL
>>> import PIL
>>> import _imaging
No errors!
How to remove ALL jpeg packages?
$ port installed | grep -i jpeg
jpeg #8c_0
jpeg #9a_0 (active)
$ sudo port -f uninstall jpeg #8c_0
$ sudo port -f uninstall jpeg #9a_0
Don't care about dependies. Beacause we need to install jpeg package again:
$ sudo port install jpeg
I ran into all the errors you guys have mentioned. I broke down and just used virtualenv and installed Pillow instead. it worked:
sudo pip install virtualenv
virtualenv python_script && cd !$
. /activate/bin
pip install Pillow
I had the same problem, with Python 2.7 and OSX Lion, and basically followed #ApPeL process and re-installed libjpeg and PIL. libjpeg seemed to be installed correctly, and PIL seemed to find it correctly, but running python -v and then import _imaging gave always this error:
ImportError:
dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/_imaging.so,
2): Symbol not found: _jpeg_resync_to_restart
After installing and re-installing libjpeg (version 8d) and PIL (version 1.1.7) twenty times with slightly different options, what did the trick was making sure I removed all libjpeg files under /usr/local/include (headers), as well as the files under /user/local/lib.
I didn't need to install PIL from source, I used pip install pil
This worked for me-
http://www.thetoryparty.com/2010/08/31/pil-on-snow-leopard-_jpeg_resync_to_restart-error/
I have been attempting to install PIL (OS X 10.7.5, Python 2.7.3) for about 5 hours. I too have been bogged down with the 'Symbol not found: _jpeg_resync_to_restart" error and have tried many of the proposed solutions to no avail, including reinstalling all of its dependencies. Finally, I discovered a double-clickable installation of Pillow.
Thank you Rudix! Now "import PIL" and "import _imaging" work!
p.s. I had installed libjpeg via i didn't specifically delete this install, so I'm not sure if this was part of the final solution or not.

Categories

Resources