Installing PIL in Snow Leopard (_jpeg_resync_to_restart error)

In earlier posts I talked about my troubles with getting my Snow Leopard (Mac OS X 10.6) system fully equipped for work purposes:

In the latest (and hopefully last) part of that saga, I tried to tackle a misbehaving Python Imaging Library (PIL) today. Earlier, I had already spent some hours trying to fix this problem but today I finally managed to overcome this problem. There are plenty of articles/blog posts already covering this problem and going over the installation steps. However, no matter how I followed these steps, it would not work.

I discovered the problem when trying to upload a picture through the Django Admin pages and received a standard Django error template stating that "The _imaging C module is not installed". Digging into this problem a little deeper I found that import the imaging library lists an error. Try this in your terminal:

 

brambraakman$ python
Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _imaging

This lead to an error that goes something like this:

>>> import _imaging

File "", line 1, in
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so
Expected in: dynamic lookup

Obviously there was something wrong with the jpeg support (libjpeg) in the imaging library. This _jpeg_resync_to_restart error has been encountered by many others as well:

However, whatever way I tried installing libjpeg (download) and PIL, nothing was working. I also had a libjpeg version installed through Fink but the PIL installation never referenced that.

Running:

otool -L /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so

will give you an overview of the dependencies of _imaging.so. It should return a reference to the libjpeg library.

Solution

I will not repeat the installation instructions for libjpeg and PIL again (you can find them in the links above). In the end I ended up using the new jpeg-7 library and using these instructions for installation.

What was wrong with my installation?

It turned out that the changes I made to install MySQLdb succesfully on my system had caused this problem. I had added:

export CC="gcc-4.0"
export CXX="g++-4.0"

to my .bash_profile which forces the system (at least that is how I understand it) to use an older version of the gcc compiler. Somehow this caused libjpeg (or PIL) to install incorrectly. Removing those lines from my .bash_profile and reinstalling libjpeg and PIL (for the nth time) finally resolved the problem.

Disclaimer
I am relatively new to Mac OS X (and UNIX based programs and libraries) and might sometimes make mistakes that I am unaware of. The above fixed the problem for me and could very well be affecting your system as well. That does not mean that you should take my advice as expert advice or that what I think are related steps and solutions are always correct. You are welcome to correct (and educate) me by leaving a comment.

  • Enjoyed this post? Please share it with others:
  • del.icio.us Favicon
  • Digg Favicon
  • Facebook Favicon
  • Google Favicon
  • LinkedIn Favicon
  • Reddit Favicon
  • Technorati Favicon
  • TwitThis Favicon
17 October 2009 • Apple, Django, Geek stuff

Comments

Pranab  on 10 Dec 2009 wrote:

I think it would be great if you posted code in your posts using code blocks, or a mono-spaced font rather than in italics.

import _imaging 


One issue is that spaces are currently not visible immediately - import _imaging looks like import_imaging, when wrapping [ code ] [ /code ] around them immediately improves it!


Bram Braakman  on 10 Dec 2009 wrote:

@Pranab - Could not agree more with you! Thanks for the tip. I made the adjustments. Should be better now.
(now I still need to fix this “comment”  formatting…)


Tory  on 31 Aug 2010 wrote:

Fantastic. I can’t thank you enough.

I had to do a few other steps…

- manual libjpeg removal
- checking system link to gcc 4.2;
- omitting an ARCHFLAGS=”-arch x86_64” value set in .profile

...but it was your guidance that saved the day.

For those still struggling, full documentation of the other steps I took is here:

http://www.thetoryparty.com/wp/2010/08/31/pil-on-snow-leopard-_jpeg_resync_to_restart-error/

THANK YOU!


Bram Braakman  on 31 Aug 2010 wrote:

@Tory Thanks for your positive feedback. It feels great to get something working after struggling with the installation.

Thanks for the link and writing up your own experience for others to benefit from.

Cheers, Bram


Esben  on 07 Nov 2010 wrote:

Could you port what you get from

otool -/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so 

I get:

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so (architecture ppc):
    /
usr/lib/libmx.A.dylib (compatibility version 1.0.0current version 315.0.0)
    /
usr/lib/libSystem.B.dylib (compatibility version 1.0.0current version 125.2.0)
/
Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so (architecture i386):
    /
usr/lib/libSystem.B.dylib (compatibility version 1.0.0current version 125.2.0

I am not sure if that is right…?


Eric  on 06 Mar 2011 wrote:

Thanks a ton for the post… I too had some issues tracking down the source of my misery, and this helped a bunch.

As a follow up… I think the *only* problem is ultimately one of code compiled for different architectures, since SL is a bit of a hybrid. My intuition says something involved with libjpeg depends mightily on being compiled for an i386 / 32-bit processor, and leaving bash to its own devices creates some headaches.

So, the resolution that worked for me: uninstall your libjpeg (in macports land, this is jpeg6b @ the moment) and PIL instances, set ‘ARCHFLAGS=“i386’ in your .bash_profile / .profile (what have you), and then reinstall the previous two (in the same order). You’ll probably want to zap that arch flag after the fact, though…


Bram Braakman  on 06 Mar 2011 wrote:

@Eric - thanks for your comment (and sharing some additional info). I am glad you figured it out.
I am already looking forward to Mac OS Lion - I can not wait to go through a lot of these kind of issues again smile


Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?

<< Back to main